View Single Post
 
Old 02-18-2017, 02:33 AM
Filip88 Filip88 is offline Windows 10 Office 2016
Novice
 
Join Date: Jan 2017
Posts: 12
Filip88 is on a distinguished road
Default Multiple Chartobjects in VBA

Hello,

Could pls anybody explain me how to refer to a chartobjects in case of multiple chartobjects are created in excel (via VBA)?

i have 1st macro, creating a chart, and the 2nd macro that should delete the same chart. Howerver i dont know how to specify or refer by the 2nd macro to this one specific chart, created by the 1st macro. When for example i create more charts in the sheet manually, the 2nd macro will delete only chart in ChartObjects(1).. i need somehow refer to only that chart, created by the 1st macro.

When i create the chart, can i somehow set fixed chartobject of the chart that will not change in case of any other multiple charts in excel?

MACRO TO CREATE CHART
Code:
Sub CreateChart()

Dim MyChart As Chart

Dim DataRange As Range
Set DataRange = ActiveSheet.Range("A1:A7")

Set MyChart = Worksheets("List1").Shapes.AddChart(x1linemarkers, 673, 250, 380, 150).Chart

MyChart.SetSourceData Source:=DataRange

With MyChart

.HasTitle = True
.ChartTitle.Text = Worksheets("List1").Range("A10")

End With
 
End If

End Sub
MACRO TO DELETE CHART
Code:
 
Sub Delete_main()

Worksheets("List1").ChartObjects(1).Delete

End If
End Sub
Thank you for help

Filip
Reply With Quote