Changing Multiple chart references within a selection

SquareHouse

New Member
Joined
Apr 26, 2021
Messages
4
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a macro that goes through all the charts in my workbook and changes the series inputs (in this case would change row 18 reference to row 19 but can obviously be amended as needed)
I'd like to amend this macro so that I can just amend the charts in my selection instead of the entire workbook
I know this must be an easy fix, but haven't had any luck in getting my tries to work

Thanks in advance

VBA Code:
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, "$18","$19")
              Next ser
      Next cht
Next sht
MsgBox("All Done")

end sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try something like this:

VBA Code:
Sub changeSelectedCharts()

    Dim cht As Object
    
    On Error Resume Next
    Set cht = ActiveChart
    On Error GoTo 0
    
    If Not cht Is Nothing Then ' one chart selected
        ChangeChart cht
        MsgBox ("All Done")
    Else
        Dim whatSelected As Object
        Set whatSelected = Selection
        If TypeName(whatSelected) = "DrawingObjects" Then
        
            For Each cht In whatSelected
            
                If TypeName(cht) = "ChartObject" Then ChangeChart cht.Chart
                
            Next cht
            
            MsgBox ("All Done")
            
        End If
    End If
End Sub
Sub ChangeChart(cht As Chart)
    Dim ser As Series
    For Each ser In cht.SeriesCollection
        ser.Formula = Replace(ser.Formula, "$18", "$19")
    Next ser
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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