Search code examples
excelvba

Moving labels on chart


I am trying to move the label for every other point in the series starting at 1, below the point, and every label for every other point starting at 2, above the point. Is there a way to 'skip' like using a regular "for..next" loop? It works for all of points if i execute the first part of the if, but I'm not sure how to make it do only every other point.

Sub MoveLabels()

  Dim objPt As Point

        For Each objPt In ActiveChart.SeriesCollection(1).Points

                objPt.DataLabel.Position = xlLabelPositionBelow
                objPt.DataLabel.HorizontalAlignment = xlCenter         
                objPt.DataLabel.VerticalAlignment = xlTop
                objPt.DataLabel.Orientation = xlHorizontal

        Next
End Sub

Solution

  • Here's what I had in mind:

    Sub MoveLabels()
    
        Dim objPt As Point
        Dim LabelPosition as xlDataLabelPosition
        LabelPosition = xlLabelPositionBelow   ' Initialize
    
        For Each objPt In ActiveChart.SeriesCollection(1).Points
    
                objPt.DataLabel.Position = LabelPosition 
                objPt.DataLabel.HorizontalAlignment = xlCenter         
                objPt.DataLabel.VerticalAlignment = xlTop
                objPt.DataLabel.Orientation = xlHorizontal
    
                ' Toggle label position
                LabelPosition = Iif(LabelPosition = xlLabelPositionBelow, xlLabelPositionAbove, xlLabelPositionBelow)
        Next
    End Sub
    

    Or you could use a FOR I = 1 to .... Points.Count and set the position based on I MOD 2