Why am I getting an overflow / # INF error when trying to position a chart label?

I have the following procedure that I use to add labels to a chart:

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

      

However, when I run sub, it breaks at an apply_to.Points(i).DataLabel.Top = apply_to.Points(i).DataLabel.Top - (10 + apply_to.Points(i).DataLabel.Height)

overflow error line . While trying to print the value apply_to.Points(i).DataLabel.Top

it looks like there is a problem retrieving the top value of the datalabel: enter image description here

However, looking at the graph, the label appears to have the correct position and should make a difference .Top

.

enter image description here

Can someone give me a pointer to what is wrong in my procedure please?

+3


source to share





All Articles