VBA: How to loop through a selection of charts?

S

spectrallypure

Hi all! I have the following little code that processes the series in
the active chart:

Sub RemoveOldSeriesInActiveChart()
ActiveChart.SeriesCollection(12).Delete
ActiveChart.SeriesCollection(11).Delete
'(...lots of other similar operations...)
End Sub

The problem is that (obviously) it only works on a single chart. I
need to extend the code to perform those operations not only on the
active chart but on all and each of the selected charts (the charts
would be selected using the mouse and holding down the CTRL key).

Unluckily enough, I haven't been able to find a way of looping through
all the charts in a selection of charts. Could somebody please give a
hint of how to accomplish this? Thanks in advance for any help! :)

Regards,

Jorge Luis.
 
B

Bob Phillips

For Each chrt In ActiveSheet.ChartObjects
chrt.Chart.SeriesCollection(12).Delete
chrt.Chart.SeriesCollection(11).Delete
Next chrt


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
J

John Bundy

Try putting your code in here, this will loop through all selected charts.
If (ActiveChart Is Nothing) Then
For Each coChartObject In Selection
'code
Next coChartObject
Else
'else code
End If
 
S

spectrallypure

Thanks so much for the replies!

Bob: unfortunately I need to perform the actions only on the selected
charts, not every chart in the current sheet.

John: oddly enough, the given code doesn't seem to work in Excel
2007!!! ...I found the case of a person with the very same problem:
http://www.pcreview.co.uk/forums/thread-3198875.php

....any further ideas?

Regards,

Jorge Luis.
 
B

Bob Phillips

For Each chrt In Selection
chrt.Chart.SeriesCollection(10).Delete
chrt.Chart.SeriesCollection(11).Delete
Next chrt


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
S

spectrallypure

For Each chrt In Selection
chrt.Chart.SeriesCollection(10).Delete
chrt.Chart.SeriesCollection(11).Delete
Next chrt

I thought so, but execution fails with "Run-time error 1004: - Invalid
parameter"... seems we're getting closer though...

Maybe it has to do to the fact that the ".Delete" is a method rather
than a property? How can I determine which type of object will "chrt"
be in the above code? ...I remember to have read somewhere that this
information (i.e. the type of a particular element in a VBA code) can
be obtained, but I don't recall where nor how...

Thanks in advance for any further ideas!

Regards,

Jorge.
 
S

spectrallypure

Hello all!

I still haven't figured this one out... I would be really grateful is
someone could throw any ideas over this issue. Thanks in advance for
any help.

Regards,

Jorge Luis.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top