选择交替点并将标签移动到/在上方 [英] Select alternate points and move labels above/below
问题描述
我对VBA来说是相当新鲜的,并尝试选择交替点以将数据标记放在上方和下方。
I am fairly new to VBA and trying to select alternating points to place datalabels above and below.
这是我的代码,正在将数据标签放在点1之下我想要的是,但是我想要将第三点的标签放在下面,另外还有其他的。我尝试了许多不同的循环和代码,但似乎没有任何工作,我不知道为什么似乎复制和粘贴,而不是移动标签。
Here is my code that is currently placing a datalabel below point 1 which I want, but then I want the 3rd point's label to be placed below as well, and the other ones above. I have tried many different loops and codes but nothing seems to work and I'm not sure why it seems to copy and paste instead of move the label.
For x = 1 To ActiveChart.SeriesCollection(1).Points.Count
With ActiveChart.SeriesCollection(1).Points(x).DataLabel
.Position = xlLabelPositionBelow
.Orientation = xlHorizontal
End With
x = x + 2
Next x
For x = 2 To ActiveChart.SeriesCollection(1).Points.Count
With ActiveChart.SeriesCollection(1).Points(x).DataLabel
.Position = xlLabelPositionAbove
.Orientation = xlHorizontal
End With
x = x + 2
Next x
这是什么我的代码目前生成:
This is what my code currently produces:
这是我想要做的:
Here is what I would like it to do:
我觉得这是一件简单的事情,如果这是可能的话我会失踪。所以任何帮助将不胜感激。有可能有一个更简单的方法吗?
谢谢你提前。
I feel like it is something simple that I am missing if this is possible. So any help would be greatly appreciated. Is it possible there is an easier way? Thank you in advance.
推荐答案
这个问题似乎是你'过度迭代'x。在哪里你想要x两个,你其实是说x = x + 2,然后也说+ 1 x(这是下一步)。您可以通过更改您的For Loops来说明For x = 1 to 3 Step 2来解决上述问题。然后当您循环使用Next x时,它将添加2而不是仅1。
The problem appears to be that you are 'over-iterating' x. Where you want x to go up by two, you're actually saying "x = x + 2" and THEN also saying "+ 1 x" (which is what Next does). You could solve this above by changing your For Loops to say "For x = 1 to 3 Step 2". Then when you loop with "Next x", it will add 2 instead of just 1.
但是,我建议您像下面这样做(在我的意见)更清楚一点,你想要一个偶数x的东西,一些奇怪的x:
However, I recommend you do it like the following, as it is (in my opinion) a little clearer that you want something for an even x, and something for an odd x:
For x = 1 To ActiveChart.SeriesCollection(1).Points.Count
With ActiveChart.SeriesCollection(1).Points(x).DataLabel
If x Mod 2 = 1 Then 'If x is odd, put label below point
.Position = xlLabelPositionBelow
.Orientation = xlHorizontal
Else 'if x is even, put label above point
.Position = xlLabelPositionAbove
.Orientation = xlHorizontal
End If
End With
Next x
这篇关于选择交替点并将标签移动到/在上方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!