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
Hi, try this:
Rich (BB code):

Sub ChartObjectsDel()
  Dim i As Long, ver As Long
  ver = Val(Application.Version)
  With Sheets("My Chart").ChartObjects
    For i = .Count To 1 Step -1
      If ver > 11 Then
        .Item(i).ShapeRange.Delete
      Else
        .Item(i).Delete
      End If
    Next
  End With
End Sub
Regards,
 
Last edited:
Upvote 0
Hi, try this:
Rich (BB code):
Sub ChartObjectsDel()
 Dim i As Long, ver As Long
 ver = Val(Application.Version)
 With Sheets("My Chart").ChartObjects
   For i = .Count To 1 Step -1
     If ver > 11 Then
       .Item(i).ShapeRange.Delete
     Else
       .Item(i).Delete
     End If
   Next
 End With
End Sub
Regards,


ZVI, I tried your suggested method, but I recieve an error.
 
Upvote 0
Yea, previous code was for Excel 2003/2007.
Not sure this is good idea, but the code below works for Excel 2010 as well.
Rich (BB code):

Sub ChartObjectsDelete()
  Dim i As Long, ver As Long, tmp As String
  ver = Val(Application.Version)
  Application.ScreenUpdating = False
  If ver >= 14 Then
    With Sheets.Add
      tmp = .Name
    End With
  End If
  With Sheets("My Chart").ChartObjects
    For i = .Count To 1 Step -1
      If ver < 12 Then
        .Item(i).Delete
      ElseIf ver = 12 Then
        .Item(i).ShapeRange.Delete
      Else  ' ver = 14
        .Item(i).Chart.Location Where:=xlLocationAsObject, Name:=tmp
      End If
    Next
  End With
  With Application
    If ver >= 14 Then
      .DisplayAlerts = False
      Sheets(tmp).Delete
      .DisplayAlerts = True
    End If
    .ScreenUpdating = True
  End With
End Sub
Regards,
 
Upvote 0
Hi Vladimir, nice to see you.

I tried the code and it did work.

I think that by now we can assume that there is really a problem with the ChartObjects collection of the Chart Sheet.
 
Upvote 0
Hi Vladimir, nice to see you.

I tried the code and it did work.

I think that by now we can assume that there is really a problem with the ChartObjects collection of the Chart Sheet.
Hi Pedro, it’s always pleasant to be in the good company here :-)
Thank you for the confirmation. Yea, the issue really looks like the bug.
I will prepare & send the ChartObjects issue report to Microsoft.
Vlad
 
Upvote 0
Yea, previous code was for Excel 2003/2007.
Not sure this is good idea, but the code below works for Excel 2010 as well.
Rich (BB code):
Sub ChartObjectsDelete()
Dim i As Long, ver As Long, tmp As String
ver = Val(Application.Version)
Application.ScreenUpdating = False
If ver >= 14 Then
 With Sheets.Add
   tmp = .Name
 End With
End If
With Sheets("My Chart").ChartObjects
 For i = .Count To 1 Step -1
   If ver < 12 Then
     .Item(i).Delete
   ElseIf ver = 12 Then
     .Item(i).ShapeRange.Delete
   Else  ' ver = 14
     .Item(i).Chart.Location Where:=xlLocationAsObject, Name:=tmp
   End If
 Next
End With
With Application
 If ver >= 14 Then
   .DisplayAlerts = False
   Sheets(tmp).Delete
   .DisplayAlerts = True
 End If
 .ScreenUpdating = True
End With
End Sub
Regards,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Wow. Thank you very much. You don't know how long I spent on this problem. :)

<o:p></o:p>
Basically the code is taking the chartobject, creating a new chart, and then deleting the new chart location. Is this correct?<o:p></o:p>
 
Upvote 0
Basically the code is taking the chartobject, creating a new chart, and then deleting the new chart location. Is this correct?
It's almost, for Excel 2010 the code creates new temporary sheet (not the Chart sheet), then moves ChartObjects to that sheet and then deletes the temporary sheet.
 
Upvote 0
It's almost, for Excel 2010 the code creates new temporary sheet (not the Chart sheet), then moves ChartObjects to that sheet and then deletes the temporary sheet.


Amazing. That is some out of the box thinking. Thanks again. I was literally having dreams (you could call them nightmares) where I was coding to fix this problem. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,348
Members
452,907
Latest member
Roland Deschain

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