2013-07-16 148 views
0

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

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

+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