VBA Excel 图表系列集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17678237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Excel Chart SeriesCollection
提问by Trenera
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
运行时错误“1004”:
应用程序定义或对象定义错误
And the help says that is an error from excel, not VBA, so it doesnt care... :) Any help will be much appreciated. Cheers!
帮助说这是来自 excel 的错误,而不是 VBA,所以它不在乎...... :) 任何帮助将不胜感激。干杯!
回答by Nathan Fisher
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