change data series for multiple charts

erickguz

Board Regular
Joined
May 11, 2010
Messages
58
Hello - I found this VBA that changes the data series for all of the charts in my workbook. Works great.

1. How do I change so that it only changes the charts on a sheet, and not the entire workbook...

2. Possible to also only change selected charts?

Thank you.


Sub ChangeAllCharts()
Dim sht As Worksheet
Dim cht As ChartObject
Dim ser As Series

For Each sht In ActiveWorkbook.Worksheets
For Each cht In sht.ChartObjects
For Each ser In cht.Chart.SeriesCollection
ser.Formula = Replace(ser.Formula, "$D$5:$D$26", "$D$5:$D$27")
Next ser
Next cht
Next sht
End Sub
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Code:
[color=darkblue]Sub[/color] ChangeAllChartsOnActiveSheet()
    [color=darkblue]Dim[/color] cht       [color=darkblue]As[/color] ChartObject
    [color=darkblue]Dim[/color] ser       [color=darkblue]As[/color] Series


    [color=darkblue]For[/color] [color=darkblue]Each[/color] cht [color=darkblue]In[/color] ActiveSheet.ChartObjects
        [color=darkblue]For[/color] [color=darkblue]Each[/color] ser [color=darkblue]In[/color] cht.Chart.SeriesCollection
            ser.Formula = Replace(ser.[color=darkblue]For[/color]mula, "$D$5:$D$26", "$D$5:$D$27")
        [color=darkblue]Next[/color] ser
    [color=darkblue]Next[/color] cht
[color=darkblue]End[/color] [color=darkblue]Sub[/color]


[color=darkblue]Sub[/color] ChangeAllChartsSelected()
    [color=darkblue]Dim[/color] cht       [color=darkblue]As[/color] ChartObject
    [color=darkblue]Dim[/color] ser       [color=darkblue]As[/color] Series


    [color=darkblue]For[/color] [color=darkblue]Each[/color] cht [color=darkblue]In[/color] Selection
        For [color=darkblue]Each[/color] ser [color=darkblue]In[/color] cht.Chart.SeriesCollection
            ser.Formula = Replace(ser.Formula, "$D$5:$D$26", "$D$5:$D$27")
        [color=darkblue]Next[/color] ser
    [color=darkblue]Next[/color] cht
[color=darkblue]End[/color] Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top