Delete multiple chartobjects from one chart sheet

userkjay

New Member
Joined
Aug 24, 2011
Messages
10
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]Sub ChartCreate()[/FONT]
[FONT=Arial]Dim chtTemp  As ChartObject[/FONT]
[FONT=Arial]Charts.Add.Location Where:=xlLocationAsNewSheet, Name:="My Chart"[/FONT]
[FONT=Arial]For j = 1 To 3[/FONT]
[FONT=Arial]   Set chtTemp = Charts("My Chart").ChartObjects.Add(4 ^ j, 4 ^ j, 10, 10)[/FONT]
[FONT=Arial]Next[/FONT]
[FONT=Arial]MsgBox Charts("My Chart").ChartObjects.Count[/FONT]
[FONT=Arial]End Sub[/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. One of the many methods of VBA code that I have tried is below:

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

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

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Let me try again:

Code:
Sub ChartDelete()
    Dim i As Long
    
    Do While ActiveChart.ChartObjects.Count
        ActiveChart.ChartObjects(1).Delete
    Loop
End Sub
 
Upvote 0
I assure you the loop executes. To prove it executes run the following. You will be promoted with the 3 chart names. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

Code:
Sub ChartDelete()
For i = 1 To ActiveChart.ChartObjects.Count
    MsgBox ActiveChart.ChartObjects(i).Name
    ActiveChart.ChartObjects(ActiveChart.ChartObjects(i).Name).Delete
Next
End Sub
 
Upvote 0
Yep. Now I'll actually test something ... :oops:
 
Upvote 0
Curious. This works in Excel 2003 on both worksheets and chart sheets, and in Excel 2007 on worksheets, but not on 2007 chart sheets:

Code:
Sub AllChartsDelete()
    Dim chObj       As ChartObject
 
    For Each chObj In ActiveSheet.ChartObjects
        chObj.Delete
    Next chObj
End Sub

I'll see If I can find someone smarter ...
 
Upvote 0
Curious. This works in Excel 2003 on both worksheets and chart sheets, and in Excel 2007 on worksheets, but not on 2007 chart sheets.


Perhaps it is a bug in the later versions of Excel. I should state that I am using Excel 2010.
 
Upvote 0
Hi

Try also:

Code:
    Charts("Chart1").ChartObjects.Delete

or

Code:
    ActiveSheet.ChartObjects.Delete
 
Upvote 0

Forum statistics

Threads
1,224,525
Messages
6,179,319
Members
452,905
Latest member
deadwings

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top