Delete multiple chartobjects from one chart sheet

Joined
Aug 24, 2011
Messages
1
Reaction score
0
The objective: Use VBA to delete multiple chartobjects from one chart sheet.

If multiple chartobjects are created on one chart using the following code:

Code:
[FONT=Arial][COLOR=black][COLOR=black][FONT=Arial]Sub ChartCreate()[/FONT][/COLOR]
 
[COLOR=black][FONT=Arial]Dim chtTemp  As ChartObject[/FONT][/COLOR]
 
[COLOR=black][FONT=Arial]Charts.Add.Location Where:=xlLocationAsNewSheet, Name:="My Chart"[/FONT][/COLOR]
 
[COLOR=black][FONT=Arial]For j = 1 To 3[/FONT][/COLOR]
[COLOR=black][FONT=Arial]   Set chtTemp = Charts("My Chart").ChartObjects.Add(4 ^ j, 4 ^ j, 10, 10)[/FONT][/COLOR]
[COLOR=black][FONT=Arial]Next[/FONT][/COLOR]
 
[COLOR=black][FONT=Arial]Msgbox Charts("My Chart").ChartObjects.Count[/FONT][/COLOR]
 
[COLOR=black][FONT=Arial]End Sub [/FONT][/COLOR]
[/COLOR][/FONT]

The 3 chartobjects on the chart “My Chart” can be selected with the mouse and deleted by hitting the delete key. I can not duplicate this using VBA. I have tried the macro recorder, but it does not capture the delete command. I have tried using sendkeys, with no success. The VBA code that I have tried is below:

Code:
[FONT=Arial][COLOR=black][COLOR=black][FONT=Arial]Sub ChartDelete()[/FONT][/COLOR]
 
[COLOR=black][FONT=Arial]For i = 1 To ActiveChart.ChartObjects.Count[/FONT][/COLOR]
[COLOR=black][FONT=Arial]   ActiveChart.ChartObjects(ActiveChart.ChartObjects(i).Name).Delete[/FONT][/COLOR]
[COLOR=black][FONT=Arial]Next[/FONT][/COLOR]
 
[COLOR=black][FONT=Arial]End Sub[/FONT][/COLOR]
[/COLOR][/FONT]


If anyone knows of a work around it would be greatly appreciated. I have found this problem to be a challenge.
 
Back
Top