VBA 中的 Excel 图表系列名称
我正在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一些问题。
首先,你总是看同一个系列!我认为您想在
If
语句中将Count
替换为a
。其次,一旦纠正了这个问题,即使您的
Name
变量在某个时候设置为True
,它也可能会在稍后重置为False
,因为For...Next
循环继续迭代系列集合的其余部分。为了防止这种情况,请添加一个Exit For
,如下所示:另外,我怀疑您没有将
Name
声明为布尔变量,因此默认情况下它是一个 Variant,因此它的值是一开始就不是False
!您应该在过程顶部使用Dim Name as Boolean
声明变量,然后如果您希望Name
默认为False
,你确实应该明确地说:将Name = False
放在循环之前。此外,Name
对于变量来说是一个糟糕的名称...Count
也是如此!我认为你的代码应该是这样的: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
witha
in yourIf
statement.Secondly, once that is corrected, even if your
Name
variable gets set toTrue
at some point, it may get reset toFalse
later as theFor...Next
loop continues iterating through the remainder of the series collection. To prevent this, add anExit For
like this:Also, I suspect you haven't declaring
Name
as a Boolean variable, so by default it's a Variant so its value isn'tFalse
to start with! You should declare the variable at the top of your procedure withDim Name as Boolean
, and then if you wantName
to beFalse
by default, you should really say so explicitly: PutName = False
before the loop. Moreover,Name
is a terrible name for a variable... and so isCount
Argh! I think your code should look like this: