Hello Glyndwr37,
Let me briefly provide an answer to your question and then explain how
to look up the answer in Excel / Visual Basic.
In your question - you refer to
ActiveSheet.ChartObjects("Chart 5").Activate
Let's revise that slightly to something like
ActiveSheet.ChartObjects.Count
which refers to the number of charts embedded on the active sheet. You
can then use that in a FOR loop like:
Sub x()
For i = 1 To ActiveSheet.ChartObjects.Count
ActiveSheet.ChartObjects(i).Name = i
Next i
End Sub
which changes the name of each chart to its index in ChartObjects
(e.g., 1, 2, 3, ...).
I found that method through the following steps:
[0] Start with a spreadsheet that has more than one chart on it.
[1] Used Tools -> Macro -> Visual Basic Editor (menu selection)
[2] Used View -> Object Browser and
View -> Immediate Window (menu selections)
[3] The object browser has a small search entry field. I entered
Chart
clicked on Excel ... Chart in the window below, and then clicked on
the [?] button in the upper right to bring up the on line help for
Chart as defined in Excel.
I saw an illustration showing the relationship between Worksheets,
ChartObjects, and Chart. Clicking on ChartObjects brings up the
ChartObjects help. At the top is two more links, one to Properties and
the other to Methods. Looking at properties, I see Count (which is
described as the number of the object - in this case ChartObjects).
I checked using the immediate window using
print ActiveSheet.ChartObjects.Count
which on my spreadsheet was "3" and using
print ActiveSheet.ChartObjects(1).Name
which on my spreadsheet was "Chart 1" (and after I ran the subroutine was 1).
From that point, the rest is pretty straight forward. Arrays such as
ChartObjects start with index 1 and go up to Count items. In that way,
you can refer to each chart and make the appropriate modifications. I
wrote the small subroutine (as shown above) to check to make sure it
works OK.
This should give you a method to:
- count the number of charts on each worksheet
- loop through the charts by using index (and not the name)
Please use a clarification request if you need any further explanation
or have problems with the answer provided.
--Maniac |