VBA Excel Chart SeriesCollection

限于喜欢 提交于 2019-12-13 03:48:09

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/17678237/vba-excel-chart-seriescollection

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!