VBA 中的 Excel 图表系列名称

发布于 2024-11-27 20:55:32 字数 570 浏览 0 评论 0原文

我正在为 Excel xy-Diagramm 编写 Makro 程序 该图表几乎是正确的,但我有重复的 DataSeriesNames; 我已经尝试浏览所有系列并比较名称。 没有错误,但它不起作用。

代码是这样的:

For a=1 to Count
  If ActiveChart.SeriesCollection(Count).Name = Range("A2").Value Then
    Name = true
  End If
  a = a + 1
Next

If Name = false Then
  ActiveChart.SeriesCollection.NewSeries
End If
ActiveChart.SeriesCollection(Count).Name = "='Tasks'!$D$" & i
ActiveChart.SeriesCollection(Count).XValues = "='Tasks'!$B$" & i
ActiveChart.SeriesCollection(Count).Values = "='Tasks'!$C$" & i

Mfg Robin

I'm programming a Makro for a Excel xy-Diagramm
The diagramm is nearly correct, but i have dublicate DataSeriesNames;
I already tried to go through all Series and Compare the Names.
There were no Errors, but it didn't work.

Code was like that:

For a=1 to Count
  If ActiveChart.SeriesCollection(Count).Name = Range("A2").Value Then
    Name = true
  End If
  a = a + 1
Next

If Name = false Then
  ActiveChart.SeriesCollection.NewSeries
End If
ActiveChart.SeriesCollection(Count).Name = "='Tasks'!$D$" & i
ActiveChart.SeriesCollection(Count).XValues = "='Tasks'!$B$" & i
ActiveChart.SeriesCollection(Count).Values = "='Tasks'!$C$" & i

Mfg Robin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱々樱花落 2024-12-04 20:55:32

这里有一些问题。

首先,你总是看同一个系列!我认为您想在 If 语句中将 Count 替换为 a

If ActiveChart.SeriesCollection(a).Name

其次,一旦纠正了这个问题,即使您的 Name 变量在某个时候设置为 True,它也可能会在稍后重置为 False,因为For...Next 循环继续迭代系列集合的其余部分。为了防止这种情况,请添加一个 Exit For ,如下所示:

For a = 1 To Count
  If ActiveChart.SeriesCollection(a).Name = Range("A2").Value Then
    Name = True
    Exit For
  End If
  a = a + 1
Next

另外,我怀疑您没有将 Name 声明为布尔变量,因此默认情况下它是一个 Variant,因此它的值是一开始就不是False!您应该在过程顶部使用 Dim Name as Boolean 声明变量,然后如果您希望 Name 默认为 False,你确实应该明确地说:将 Name = False 放在循环之前。此外,Name 对于变量来说是一个糟糕的名称...Count 也是如此!我认为你的代码应该是这样的:

Option Explicit

Sub MySub()
    Dim a As Long
    Dim NameExists As Boolean
    Dim SeriesCount As Long

    SeriesCount = ActiveChart.SeriesCollection.Count

    NameExists = False
    For a = 1 To SeriesCount 
      If ActiveChart.SeriesCollection(a).Name = Range("A2").Value Then
          NameExists = True
          Exit For
      End If
      a = a + 1
    Next

    If NameExists = False Then
    ' Rest of your code goes here...

End Sub

There are a couple of things wrong here.

First of all, you're always looking at the same series! I think you want to replace Count with a in your If statement.

If ActiveChart.SeriesCollection(a).Name

Secondly, once that is corrected, even if your Name variable gets set to True at some point, it may get reset to False later as the For...Next loop continues iterating through the remainder of the series collection. To prevent this, add an Exit For like this:

For a = 1 To Count
  If ActiveChart.SeriesCollection(a).Name = Range("A2").Value Then
    Name = True
    Exit For
  End If
  a = a + 1
Next

Also, I suspect you haven't declaring Name as a Boolean variable, so by default it's a Variant so its value isn't False to start with! You should declare the variable at the top of your procedure with Dim Name as Boolean, and then if you want Name to be False by default, you should really say so explicitly: Put Name = False before the loop. Moreover, Name is a terrible name for a variable... and so is Count Argh! I think your code should look like this:

Option Explicit

Sub MySub()
    Dim a As Long
    Dim NameExists As Boolean
    Dim SeriesCount As Long

    SeriesCount = ActiveChart.SeriesCollection.Count

    NameExists = False
    For a = 1 To SeriesCount 
      If ActiveChart.SeriesCollection(a).Name = Range("A2").Value Then
          NameExists = True
          Exit For
      End If
      a = a + 1
    Next

    If NameExists = False Then
    ' Rest of your code goes here...

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文