vba 如何在VBA中更改系列名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31541179/
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
How to change series name in VBA
提问by Cam
I have a series of charts I am creating using VBA (code below).
我有一系列使用 VBA 创建的图表(下面的代码)。
I am having trouble changing the names of the series from series 1 and series 2 to Current State and Solution State.
我无法将系列名称从系列 1 和系列 2 更改为当前状态和解决方案状态。
I keep getting an
我不断收到
Object Variable or With Block Variable not set
未设置对象变量或带块变量
error.
错误。
However without the srs1and srs2code the charts work just fine (just with the wrong series names).
但是,如果没有srs1和srs2代码,图表就可以正常工作(只是使用了错误的系列名称)。
I looked up how to fix this and the answer I received however is not working for me.
Does anyone know another way to do this?
我查找了如何解决这个问题,但我收到的答案对我不起作用。
有谁知道另一种方法来做到这一点?
Sub MA()
Dim Srs1 As Series
Dim Srs2 As Series
Dim i As Integer
Dim MAChart As Chart
Dim f As Integer
f = 2 * Cells(2, 14)
For i = 1 To f Step 2
Set MAChart = ActiveSheet.Shapes.AddChart(Left:=750, Width:=400, Top:=130 + 50 * (i - 1), Height:=100).Chart
With MAChart
.PlotBy = xlRows
.ChartType = xlColumnClustered
.SetSourceData Source:=ActiveSheet.Range("Q" & 1 + i & ":Z" & 2 + i)
.Axes(xlValue).MaximumScale = 4
.Axes(xlValue).MinimumScale = 0
.HasTitle = True
.ChartTitle.Text = "Provider Load for " & Cells(i + 1, 15)
'where errors start- works fine up to this point
Set Srs1 = ActiveChart.SeriesCollection(1)
Srs1.Name = "Current State"
Set Srs2 = ActiveChart.SeriesCollection(2)
Srs2.Name = "Proposed Solution"
End With
Next i
End Sub
回答by 3-14159265358979323846264
Try changing these lines ...
尝试更改这些行...
Set Srs1 = ActiveChart.SeriesCollection(1)
Srs1.Name = "Current State"
Set Srs2 = ActiveChart.SeriesCollection(2)
Srs2.Name = "Proposed Solution"
To ...
到 ...
.SeriesCollection(1).Name = "Current State"
.SeriesCollection(2).Name = "Proposed Solution"
You are already using MAChartinside your With block so you should be able to access it's .SeriesCollection(x).Nameproperties in the same fashion as you have done for the other properties.
您已经MAChart在 With 块中使用,因此您应该能够以.SeriesCollection(x).Name与其他属性相同的方式访问它的属性。
回答by Juliusz
I believe the problem is with referencing - in the code you reference ActiveChart (I am guessing it does not exist), while you have created MAChart in the code above.
我相信问题在于引用 - 在您引用 ActiveChart 的代码中(我猜它不存在),而您在上面的代码中创建了 MACChart。
Set Srs1 = MAChart.SeriesCollection(1)
Srs1.Name = "Current State"
Set Srs2 = MAChart.SeriesCollection(2)
Srs2.Name = "Proposed Solution"

