Math with DataLabel.Caption

marksc92

New Member
Joined
Sep 4, 2014
Messages
10
It has been a while since I have been this stumped.. I am trying to compare points values in 2 series so I can adjust the position of data labels so they do not overlap. This works well for all but one of my charts, which after some troubleshooting I learned is because it is evaluating them as strings so comparing 70 and 80 is easy but it doesn't give me the result I am looking for when comparing 95 to 105, it thinks 95 is bigger because 9 > 1. Does anyone know a way to get the point value or convert the DataLabel.Caption to a number. Int(), Cint() didn't work. Here is the code


Code:
Sheet3.ChartObjects("MS").Activate ' Activate MS Chart


With ActiveChart
        ' Loop through data points in Series 2 and 3
        For J = 1 To .SeriesCollection(2).Points.Count
        
        '' If the point in series 2 is larger than the point in series 3,
        '' put 2's label above the line, and 3's below the line,
        '' otherwise, do the opposite.
        
            If .SeriesCollection(2).Points(J).DataLabel.Caption > .SeriesCollection(3).Points(J).DataLabel.Caption Then
            .SeriesCollection(2).Points(J).DataLabel.Position = xlLabelPositionAbove
            .SeriesCollection(3).Points(J).DataLabel.Position = xlLabelPositionBelow
            Else
            .SeriesCollection(3).Points(J).DataLabel.Position = xlLabelPositionAbove
            .SeriesCollection(2).Points(J).DataLabel.Position = xlLabelPositionBelow
           End If
        Next J ' Loop through next set of points
End With
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I honestly have no idea if this will help, but I figure its worth a shot. Stores the captions as an intermediate variable and then casts them as 'long' datatype for comparison.


Code:
Sheet3.ChartObjects("MS").Activate ' Activate MS Chart

Dim labelPosA As String
Dim labelPosB As String

With ActiveChart

        ' Loop through data points in Series 2 and 3
        For J = 1 To .SeriesCollection(2).Points.Count
        
        '' If the point in series 2 is larger than the point in series 3,
        '' put 2's label above the line, and 3's below the line,
        '' otherwise, do the opposite.
        
        labelPosA = .SeriesCollection(2).Points(J).DataLabel.Caption
        labelPosB = .SeriesCollection(3).Points(J).DataLabel.Caption
        
            If CLng(labelPosA) > CLng(labelPosB) Then

            .SeriesCollection(2).Points(J).DataLabel.Position = xlLabelPositionAbove
            .SeriesCollection(3).Points(J).DataLabel.Position = xlLabelPositionBelow

            Else

            .SeriesCollection(3).Points(J).DataLabel.Position = xlLabelPositionAbove
            .SeriesCollection(2).Points(J).DataLabel.Position = xlLabelPositionBelow

           End If

        Next J ' Loop through next set of points

End With
 
Upvote 0
Clever, but no that did not work. I also tried Dim as Long, and multiplying the value by 1, neither worked. Thanks for trying. I'll keep banging my head against the wall until I figure it out
 
Upvote 0
I got it figured out.


Code:
Sheet3.ChartObjects("MS").Activate ' Activate MS Chart


Dim X As Variant
Dim Y As Variant


With ActiveChart


        ' Loop through data points in Series 2 and 3
        X = .SeriesCollection(2).Values
        Y = .SeriesCollection(3).Values
        
        For J = 1 To UBound(X)
        
        '' If the point in series 2 is larger than the point in series 3,
        '' put 2's label above the line, and 3's below the line,
        '' otherwise, do the opposite.
        
        
            If X(J) > Y(J) Then
            .SeriesCollection(2).Points(J).DataLabel.Position = xlLabelPositionAbove
            .SeriesCollection(3).Points(J).DataLabel.Position = xlLabelPositionBelow
            Else
            .SeriesCollection(3).Points(J).DataLabel.Position = xlLabelPositionAbove
            .SeriesCollection(2).Points(J).DataLabel.Position = xlLabelPositionBelow
            End If
            
            
        Next J ' Loop through next set of points


End With
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top