简体   繁体   中英

VBA Excel Chart SeriesCollection

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM