我有以下程序,用于向图表添加标签:
Sub add_comments(apply_to As Series, source_range As Range)
Dim i As Long
Dim c As Range
If source_range.Count > apply_to.Points.Count Then
Set source_range = source_range.Resize(apply_to.Points.Count, 1)
End If
i = 1
For Each c In source_range
If Not IsError(c) And i <= apply_to.Points.Count Then
If Len(c.Text) <> 0 Then
apply_to.Points(i).HasDataLabel = True
apply_to.Points(i).DataLabel.Text = c.Value2
apply_to.Points(i).DataLabel.Format.AutoShapeType = msoShapeRectangle
With apply_to.Points(i).DataLabel.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 0, 0)
End With
apply_to.Points(i).DataLabel.Position = xlLabelPositionAbove
Debug.Print apply_to.Points(i).DataLabel.Top
apply_to.Points(i).DataLabel.Top = apply_to.Points(i).DataLabel.Top - (10 + apply_to.Points(i).DataLabel.Height)
Else
If apply_to.Points(i).HasDataLabel Then
apply_to.Points(i).DataLabel.Delete
End If
End If
End If
i = i + 1
Next c
apply_to.HasLeaderLines = True
End Sub
但是,当我运行sub时,它会在行apply_to.Points(i).DataLabel.Top = apply_to.Points(i).DataLabel.Top - (10 + apply_to.Points(i).DataLabel.Height)
上中止并出现溢出错误。尝试打印apply_to.Points(i).DataLabel.Top
的值会使得获取datalabel的最高值看起来有问题:
但是,查看图表时,标签似乎有一个有效的位置,并且应该有一个.Top
值。
有人可以给我一个指向我程序中出错的指针吗?