我正在使用 VBA 创建一系列图表(代码如下)。
我在将系列名称从系列 1 和系列 2 更改为当前状态和解决方案状态时遇到问题。
我不断收到
Object Variable or With Block Variable not set
错误。
但是,如果没有 srs1
和 srs2
代码,图表就可以正常工作(只是系列名称错误)。
我查找了如何解决此问题,但收到的答案对我不起作用。
有谁知道另一种方法可以做到这一点?
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
最佳答案
尝试更改这些行...
Set Srs1 = ActiveChart.SeriesCollection(1)
Srs1.Name = "Current State"
Set Srs2 = ActiveChart.SeriesCollection(2)
Srs2.Name = "Proposed Solution"
致...
.SeriesCollection(1).Name = "Current State"
.SeriesCollection(2).Name = "Proposed Solution"
您已经在 With block 中使用了 MAChart
,因此您应该能够以与您所做的相同的方式访问它的 .SeriesCollection(x).Name
属性对于其他属性。
关于vba - 如何在VBA中更改系列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31541179/