vba - 如何在VBA中更改系列名称

标签 vba excel charts series

我正在使用 VBA 创建一系列图表(代码如下)。

我在将系列名称从系列 1 和系列 2 更改为当前状态和解决方案状态时遇到问题。

我不断收到

Object Variable or With Block Variable not set

错误。

但是,如果没有 srs1srs2 代码,图表就可以正常工作(只是系列名称错误)。

我查找了如何解决此问题,但收到的答案对我不起作用。
有谁知道另一种方法可以做到这一点?

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/

相关文章:

excel - 排序和删除行。运行时错误 '13' : Type mismatch

Excel - 动态图表 x 轴 - 忽略没有数据的 x 类别

r - 如何在 Excel VBA 中复制 R 子集机制?

vba - 简单 VBA 矩阵代码中的类型不匹配错误

c# - 可以推荐 .NET 的 ZedGraph 图表库吗?

jquery - 具有多个数据和各个数据点作为图例的堆叠条形图

Excel CurrentProject.Path,运行时错误 424

excel - 循环遍历整个工作簿并删除具有 #N/A 的行

java - JExcel API : how to change the color of a Cell?

javascript - 如何获取先前创建的 Google Chart 的实例?