tags:

views:

52

answers:

2

I was trying to chart some RTD data and accidentally ended up with hundreds of charts on the same worksheet. Now I'm trying to undo my error and remove all of the charts but I'm not having much luck with it.

I recorded a macro where I deleted one of the charts manually and then tried editing the code to loop through all of the charts but I keep getting an error. My code is below:

Sub Macro3()

Dim i As Integer

For i = 1 To 100
  Sheets("Calculations").Select
  ActiveSheet.ChartObjects("Chart " & CStr(i)).Activate
  ActiveChart.ChartArea.Select
  ActiveWindow.Visible = False
  Selection.Delete
Next i
End Sub

When I try running this, I get an error saying that the ChartObjects property was inaccessible from the Worksheet class.

I'm sure there's a simple explanation/solution to this but I've learned that VBA sometimes does things a little differently than you might be expecting. So, I guess my question is, how do I remove the charts without having to go through each one at a time?

Any help would be appreciated. Thanks.

+1  A: 

It's quite possible you are attempting to access a chart by name which no longer exists. Try accessing the charts by index using ChartObjects(i) instead.

Conspicuous Compiler
Thank you very much. I guess that was the problem. I still ended up getting the error a couple times but eventually I got rid of all the charts. Phew. Thanks again
Shaka
+1  A: 

Try this, it will get rid of all charts on your sheet regardless of their names.

Sub Macro3()
    Worksheets("Calculations").ChartObjects.Delete
End Sub
dtjohnso
Awesome! Works like a charm. Thanks.
Shaka