PDA

View Full Version : Solved: How to Exlcude some charts from Macro



Nick_London
08-25-2010, 09:16 AM
Hello All,

I have the following macro below that I use to update all charts on the activesheet however I only want to apply it most charts but not all. Is there a way I can exclude it running on specified charts.

For example I want to exclude the charts below from being impacted by the macro:

ActiveSheet.ChartObjects("Chart 8")
ActiveSheet.ChartObjects("Chart 17")

Is it possible to do this?

Many thanks for help in advance,

Nick



Sub Refresh_Charts_test2()

Dim ch As ChartObject
Dim i As Long

For Each ch In ActiveSheet.ChartObjects
ch.Select
For i = 1 To ActiveChart.SeriesCollection.Count

ActiveChart.SeriesCollection(i).Values = Worksheets("Data").Range(D10: D120)
ActiveChart.SeriesCollection(i).XValues = Worksheets("Data").Range(A10:A120)

Next i 'series in the chart
Next ch 'next chart
End Sub

Bob Phillips
08-25-2010, 09:50 AM
Sub Refresh_Charts_test2()

Dim ch As ChartObject
Dim i As Long

For Each ch In ActiveSheet.ChartObjects

With ch

If .Name <> "Chart 8" And .Name <> "Chart 17" Then

For i = 1 To .SeriesCollection.Count

.SeriesCollection(i).Values = Worksheets("Data").Range("D10:D120")
.SeriesCollection(i).XValues = Worksheets("Data").Range("A10:A120")

Next i 'series in the chart
End If
End With
Next ch 'next chart
End Sub

Nick_London
08-26-2010, 02:41 AM
Hi XLD,

Thanks for the adjusted code. However when I run this it generates a run time error 438 saying that teh Object does not suport this property or Object. Decoding takes me to the line:

For i = 1 To .SeriesCollection.Count

Any thoughts?

Bob Phillips
08-26-2010, 02:45 AM
Can you post your workbook for me to see?

Nick_London
08-26-2010, 03:44 AM
Attached.

Nick

Bob Phillips
08-26-2010, 04:34 AM
Sub xldRefresh_Charts_test2()
Dim ch As ChartObject
Dim i As Long
For Each ch In ActiveSheet.ChartObjects
With ch
If .Name <> "Chart 8" And .Name <> "Chart 17" Then
For i = 1 To .Chart.SeriesCollection.Count
.Chart.SeriesCollection(i).Values = Worksheets("Data").Range("D10:D120")
.Chart.SeriesCollection(i).XValues = Worksheets("Data").Range("A10:A120")
Next i 'series in the chart
End If
End With
Next ch 'next chart
End Sub

Nick_London
08-27-2010, 03:22 AM
Thanks XLD.

I've tested the ammended code out on a couple of datasets and it works great.

Thanks,

Nick