VBA Excel Chart SeriesCollection

时间:2013-07-16 13:44:55

标签: excel vba charts

我在最近几个小时因为错误而苦苦挣扎。 我想制作一个代码,为每张纸绘制相同的范围。 当我添加一个系列集合时,它失败了。 我已经修改了录制的宏中的代码,它完美地运行。 这是有问题的代码:

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

错误是:

运行时错误'1004':
应用程序定义或对象定义的错误

帮助说这是excel的错误,而不是VBA,所以它并不关心...... :) 任何帮助都感激不尽。 干杯!

1 个答案:

答案 0 :(得分: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