此代码接受图表和最后一个数据点,然后对所有系列的数据点.top进行排序,并添加12.5(数据标签的大小)以确保它们不重叠。 问题是如果我没有等待调用暂停宏,行
dLabels(i, 1) = .SeriesCollection(i).Points(lastpoint).DataLabel.Top
dLabels(i, 2) = i
会给我一个-1.#IND
的值,但是如果我单步执行它或者"暂停"一切都很好
oSh
是Shape
,lastpoint
是Long
。
宏只是工作太快而无法从标签中获取值吗?
Sub MoveLabels(oSH, lastpoint)
Dim ch As Chart
Dim i As Long
Dim dLabels() As Double
*Call Wait(3)* ' just pauses the macro for less than a second
If oSH.Name = "SCG_US" Then
With oSH.Chart
' the -2 will only allow it to move the labels that are not static (the last two in the series)
ReDim dLabels(1 To .SeriesCollection.Count - 2, 2)
For i = 1 To .SeriesCollection.Count - 2
**dLabels(i, 1) = .SeriesCollection(i).Points(lastpoint).DataLabel.Top**
dLabels(i, 2) = i
'dLabels(i, 3) = .Chart.SeriesCollection(i).Values(lastpoint)dLabels(2, 1)
Next
Call BubbleSort(dLabels) 'simple sort of array
' This section sets top values for all data labelsto be at least 12.5 units apart
For j = 2 To .SeriesCollection.Count - 2
**If dLabels(j - 1, 1) + 12.5 > dLabels(j, 1) Then**
dLabels(j, 1) = dLabels(j, 1) + (12.5 - (dLabels(j, 1) - dLabels(j - 1, 1)))
End If
Next j
Sub Wait(waitTime)
Start = Timer
While Timer < Start + waitTime
DoEvents
Wend
End Sub
这是我改变了他们所处的顺序的调用语句:
If .SeriesCollection.Count > 1 Then Call MoveLabels(oSH, 1) 'moves first label
If .SeriesCollection.Count > 1 And lastpoint <> 999 Then Call MoveLabels(oSH, lastpoint) ' moves last label