0
votes

I'm struggling for the last couple of hours with an error. I want to make a code that plots the same range for each sheet. When I add a series collection it fails. I have modified the code from a recorded macro, which works perfectly. This is the code in question:

Sub plot()

Dim wb As Excel.Workbook
Set wb = ThisWorkbook
Dim ws As Worksheet
Dim name As String
Dim plot As Excel.Shape

For Each ws In wb.Worksheets
name = ws.name
Set plot = ws.Shapes.AddChart
plot.Chart.ChartType = xlXYScatterLines'until here it works perfectly
plot.Chart.SeriesCollection(1).name = "=""something"""' on this line I get the error
Next

End Sub

And the error is:

Run - time error '1004':
Application-defined or object-defined error

And the help says that is an error from excel, not VBA, so it doesnt care... :) Any help will be much appreciated. Cheers!

1
There is no series to name if there is no data in the chart.Jon Peltier

1 Answers

2
votes

I think you need to add

plot.Chart.SetSourceData Range("A1", "D4")

just below the line that works perfectly, so you end up with this

Sub plot()

Dim wb As Excel.Workbook
Set wb = ThisWorkbook
Dim ws As Worksheet
Dim name As String
Dim plot As Excel.Shape

For Each ws In wb.Worksheets
name = ws.name
Set plot = ws.Shapes.AddChart
plot.Chart.ChartType = xlXYScatterLines 'until here it works perfectly
plot.Chart.SetSourceData Range("A1", "D4")
plot.Chart.SeriesCollection(1).name = "=""something""" ' on this line I get the error
Next

End Sub