It is the SeriesCollection
that needs to be indexed, not Name
. (Each collection only has one Name
).
I've tested this, but it is essentially the same as Tim's:
Sub RenameSeries()
Dim ws As Worksheet
Dim i As Integer
Set ws = ActiveSheet
'Loops through charts
For i = 1 To ws.ChartObjects.Count
With ws.ChartObjects(i).Chart
.SeriesCollection(1).Name = "Name1"
.SeriesCollection(2).Name = "Name2"
End With
Next i
End Sub
If one or more of the charts doesn't have 2 SeriesCollections, and you are happy to ignore (skip) this inconsistency, then you can use:
Sub RenameSeries()
Dim ws As Worksheet
Dim i As Integer
Set ws = ActiveSheet
'Loops through charts
For i = 1 To ws.ChartObjects.Count
With ws.ChartObjects(i).Chart
.SeriesCollection(1).Name = "Name1"
If .SeriesCollection.Count > 1 Then
.SeriesCollection(2).Name = "Name2"
End If
End With
Next i
End Sub
It is better, of course, to debug the code to discover why you have this unexpected inconsistency - perhaps a rogue chart that is hiding behind another one.
A side-note, 500 charts is a lot. I would prefer to use, perhaps, comboboxes to dynamically change the data-source(s) for fewer charts. I appreciate, however, that this requires quite a bit of Excel knowledge.