VBA Excel Chart SeriesCollection

我在最近几个小时挣扎着,发生了一个错误。 我想做一个代码,绘制每张纸的相同范围。 当我添加一个系列集合失败。 我修改了录制的macros的代码,完美的工作。 这是有问题的代码:

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,所以它不关心… :)任何帮助将不胜感激。 干杯!

我想你需要补充

 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