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

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
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,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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