Frage

Sub aaGraphing()
'
' aaGraphing Macro
'

'
    Range("L948:W949,D948:D949").Select
    Range("D949").Activate
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLineMarkers
    ActiveChart.SetSourceData Source:=Range( _
        "Analytics!$L$948:$W$949,Analytics!$D$948:$D$949")
End Sub

This code creates a chart of the data I want. Is there a way to way to make the created chart temporary so that when you click anywhere outside of the chart it deletes?

War es hilfreich?

Lösung 2

Regular module....

Option Explicit


Public PlotName As String
Public PlotRange As Range


Sub Tester()
AddPlot ActiveSheet.Range("B3:B7,D3:D7")
End Sub

Sub AddPlot(rng As Range)
    With ActiveSheet.Shapes.AddChart
        PlotName = .Name
        .Chart.ChartType = xlLineMarkers
        .Chart.SetSourceData Source:=Range(rng.Address())
    End With
    Set PlotRange = rng
    Application.EnableEvents=False    
    rng.Select
    Application.EnableEvents=True
End Sub

Sub RemovePlot(rng As Range)
    If Not PlotRange Is Nothing Then
        If Application.Intersect(rng, PlotRange) Is Nothing Then
            On Error Resume Next
            rng.Parent.Shapes(PlotName).Delete
            On Error GoTo 0
        End If
    End If
End Sub

Sheet code module:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    RemovePlot Target
End Sub

Andere Tipps

You could use the SelectionChange event of the worksheet to delete the chart. Below I am assuming there is only one shape - the chart - that might be on the worksheet.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Me.Shapes.Count = 1 Then
        Me.Shapes(1).Delete
    End If
End Sub

It is possible (I believe) to dynamically attach this event, and remove it. However, I believe that it is a little complicated.

An alternative might be to use Application.OnTime to delete it after a period of time.

Application.OnTime Now + TimeValue("00:00:40"), "ProcedureToDelete"

will run the procedure named 'ProcedureToDelete' after 40 seconds. In this procedure you might want to make sure the the selection is in the worksheet, not in the chart that you are about to delete.

You could, in the timed-procedure, check to see if the chart is currently selected. If not, delete it, otherwise set the timer again.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top