如何在VBA中更改系列名称

如何在VBA中更改系列名称

本文介绍了如何在VBA中更改系列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列使用VBA创建的图表(以下代码)。

I have a series of charts I am creating using VBA (code below).

我无法更改系列1和系列2的系列名称到当前状态和解决方案状态。

I am having trouble changing the names of the series from series 1 and series 2 to Current State and Solution State.

我不断得到

错误。

但是没有 srs1 srs2 代码可以很好地运行图表(只是使用错误的系列名称)。

However without the srs1 and srs2 code the charts work just fine (just with the wrong series names).

我抬起头来如何解决此问题,但是收到的答案对我不起作用。

有谁知道另一种方法吗?

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


推荐答案

尝试更改这些行...

Try changing these lines ...

     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块中使用了 MAChart ,因此您应该可以访问它是 .SeriesCollection(x).Name 中的属性

You are already using MAChart inside your With block so you should be able to access it's .SeriesCollection(x).Name properties in the same fashion as you have done for the other properties.

这篇关于如何在VBA中更改系列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:58