0

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!

4

1 回答 1

2

我认为您需要添加

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

就在完美运行的线下方,所以你最终得到了这个

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
于 2013-07-16T14:04:40.633 回答