Macro to delete charts

  • Thread starter Thread starter bambam77
  • Start date Start date
B

bambam77

Is it possible to create a macro that can select and delete all of the
charts in a workbook. Regardless of their names?
 
This will remove chart sheets and embedded charts:

'====================================
Sub ChartsDelete()
Dim ch As Chart
Dim ws As Worksheet
Dim chObj As ChartObject
Application.DisplayAlerts = False

For Each ch In ActiveWorkbook.Charts
ch.Delete
Next

For Each ws In ActiveWorkbook.Worksheets
For Each chObj In ws.ChartObjects
chObj.Delete
Next
Next

Application.DisplayAlerts = True
End Sub
'==================================
 
Thank you, this worked great. I have another question. I am new to thi
macro writing and I have done a few projects so far that have worked.

I have never used the Dim command, yet I see this a lot from peopl
here and in the delete chart code I just received. What is this Dim
And what does it do and when do I need it?

I know it has something to do with defining variables. I have used som
variables in some of my macros without the Dim command and they worke
fine so I'm kind of confused as to it's use.


Thank
 
Good Practice is to start your code with

OPTION EXPLICIT

then you will need to define variables with the DIM statement, but avoid
problems with duplicate names etc
 
And if you use "option explicit" and dim your variables appropriately, then you
can get the VBE's intellisense to pop up for you. (Less wear and tear on the
memory!)

Try this in a test module:

Option explicit
sub testme()
Dim Wks as worksheet
'now type wks. (as soon as you hit that dot, you should see some valid choices
for what you can do to a worksheet (or a property of the worksheet that you can
examine).

This is very nice to us older folks.

And by forcing you to declare your variables, you don't have to spend hours
looking for the mistake in this code:

for xlctr = 1 to 5
msgbox x1ctr
next xlctr

(the one's and ell's are mixed up).

You would have gotten an error when you tried to compile.

And this is nice to us older folks with bifocals!
 
Back
Top