VBA to change all chart legend texts from a to b

BuJay

Board Regular
Joined
Jun 24, 2020
Messages
75
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
Platform
  1. Windows
Assume I have a few hundred charts in an excel workbook. Let's assume there are two lines on each chart - 1 line series is named AAA and therefore, the legend for this series shows AAA. The other series is named NonAAA and so, the legend labels this series as NonAAA.

Is there a way to use VBA to go through every single chart and replace AAA in each series name with BBB - or alternatively, is there a way to change the text labels in all legends so that they say BBB and NonBBB for all charts?

Thanks!
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
There seems to be a solution here that looks like

Try changing these lines ...

Set Srs1 = ActiveChart.SeriesCollection(1)
Srs1.Name = "Current State"
Set Srs2 = ActiveChart.SeriesCollection(2)
Srs2.Name = "Proposed Solution"

To ...

.SeriesCollection(1).Name = "Current State"
.SeriesCollection(2).Name = "Proposed Solution"
 
Upvote 0
There seems to be a solution here that looks like

Try changing these lines ...

Set Srs1 = ActiveChart.SeriesCollection(1)
Srs1.Name = "Current State"
Set Srs2 = ActiveChart.SeriesCollection(2)
Srs2.Name = "Proposed Solution"

To ...

.SeriesCollection(1).Name = "Current State"
.SeriesCollection(2).Name = "Proposed Solution"
That isn't really what I am looking for...I'll try to provide additional info
 
Upvote 0
I have this code that works to update the actual series references. However, this is not what I need.

Sub updateChart()

Dim sh As Worksheet
Set sh = ActiveSheet

'For each chart on the selected sheet
For Each ch In sh.ChartObjects
For Each srs In ch.Chart.SeriesCollection
'i = InStr(srs.Formula, "HFI_") + InStr(srs.Formula, "Portfolio")
i = InStr(srs.Formula, "HLSC_")
Debug.Print i

'if i is greater than 0 it means that the series has the old word
If i > 0 Then
'replace the old word with the new word
'newSrs = Replace(Replace(srs.Formula, "portfolio", "investor"), "Portfolio", "Investor")
newSrs = Replace(srs.Formula, "HLSC_", "Branch_")
Debug.Print newSrs



'update the series
srs.Formula = newSrs
End If
'next series
Next
'next chart
Next

End Sub



What I need is to change the "Names" of the series....I think the object is ActiveChart.FullSeriesCollection(1).Name

Is there a way to create a loop that says

For each chart in the worksheet,
If the ActiveChart.FullSeriesCollection(1).Name = "AAA", then change to "NotAAA"
If the ActiveChart.FullSeriesCollection(2).Name = "BBB", then change to "NotBBB"
end
 
Upvote 0
I have little experience with chart vba but think it should be possible and would be willing to dabble (which usually results in me writing code only to have someone else who's more experience beat me to the punch).
OK, while holding this post open, I wrote this, which works for my test. I think it can be simplified though but wanted to get it in here first.
VBA Code:
Sub test()
Dim i As Integer
Dim chrt As Chart

i = 1
If ThisWorkbook.Charts.Count > 0 Then
    For Each chrt In ThisWorkbook.Charts
        If InStr(1, chrt.SeriesCollection(i).Name, "Onsite") > 0 Then
            chrt.SeriesCollection(i).Name = Replace(chrt.SeriesCollection(i).Name, "Onsite", "Offsite")
        End If
    Next
End If
   
End Sub

Please use code tags (vba button on posting toolbar) to maintain indentation and readability of code.
 
Upvote 0
Since you know the series names, this also works. If you have a lot of series there might be a better way than writing a line for each one. Also, if this is going to happen frequently, then you may want to pick up the change values from inputs (cells, input box, whatever).
VBA Code:
If ThisWorkbook.Charts.Count > 0 Then
    For Each chrt In ThisWorkbook.Charts
        chrt.SeriesCollection(1).Name = Replace(chrt.SeriesCollection(1).Name, "Offsite", "Onsite")
        chrt.SeriesCollection(2).Name = Replace(chrt.SeriesCollection(2).Name, "Financial", "Financials")
        chrt.SeriesCollection(3).Name = Replace(chrt.SeriesCollection(3).Name, "Average", "Averages")
    Next
End If
 
Upvote 0
Tried this but getting an error

VBA Code:
Sub updateChart()

Dim i As Integer
Dim sh As Worksheet
Set sh = ActiveSheet

i = 1

'For each chart on the selected sheet
    For Each ch In sh.ChartObjects
        
        If InStr(1, chrt.SeriesCollection(i).Name, "Branch") > 0 Then
            chrt.SeriesCollection(i).Name = Replace(chrt.SeriesCollection(i).Name, "Branch", "AVM")
        End If
    
    Next


End Sub
 
Upvote 0
You really ought to say what the error is.
I'll guess - it's because a chart is not a worksheet - it is a sheet though. However I don't think Sheet will work, which is why I declared it as a chart.
EDIT - I think I'm mistaken about being a worksheet.
 
Last edited:
Upvote 0
Update - it could be that your sheet is a sheet of type "Chart", not a worksheet with a chart on it. The latter looks like this
1682696181934.png


a chart sheet has no gridlines, rows or columns
1682696292114.png
 
Upvote 0
Sorry @Micron - the error said something like "object not defined". The charts are not separate chart sheets - they are simply created in a sheet call "charts".
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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