Google Answers Logo
View Question
 
Q: VBA code for selecting charts in Excel ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: VBA code for selecting charts in Excel
Category: Computers > Programming
Asked by: glyndwr37-ga
List Price: $5.00
Posted: 02 Sep 2004 10:46 PDT
Expires: 02 Oct 2004 10:46 PDT
Question ID: 396070
I am looking for a VBA code example for Excel that makes the same
change to all the charts on a worksheet without knowing the number of
charts on the sheet, or the names of the charts.

As a simple example, I would like to change the names of the first two
series in all the charts on each sheet. But, I don't know how to get
around selecting the charts by name:

    ActiveSheet.ChartObjects("Chart 5").Activate
    ActiveChart.SeriesCollection(1).Name = "='Series 1'!R1C1"
    ActiveChart.SeriesCollection(2).Name = "='Series 2'!R1C1"

The crux of my troubles is figuring out how many charts there are, and
how to loop through them without the names of each one.
Answer  
Subject: Re: VBA code for selecting charts in Excel
Answered By: maniac-ga on 02 Sep 2004 16:11 PDT
Rated:5 out of 5 stars
 
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
glyndwr37-ga rated this answer:5 out of 5 stars and gave an additional tip of: $3.00
Worked perfectly! You've just saved me so much time. I also appreciate
the detail provided about how to find the answer myself. I didn't even
know about the object browser, which'll definitely help me as I hack
my way through my next problem.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy