0

I am trying to add data label only to the largest piece of a pie chart using VBA.

When I try to run the following code i get an error message: 'Object doesnt support this property of method'

The error referes to the line:

sem = ActiveSheet.ChartObjects("REJT").Chart.SeriesCollection(1).Points(x).Value

Can anyone tell me what is the issue with this line? Is there an alternative way of getting value of a chart data point?

Dim krb As Long
Dim x As Long
Dim rr As Long
Dim sem As Long
Dim xmax As Long

Set pts = ActiveSheet.ChartObjects("REJT").Chart.SeriesCollection(1).Points
krb = pts.Count
x = 1
rr = 0

'While loop to find largest part of pie chart
While x < (krb + 1)
sem = ActiveSheet.ChartObjects("REJT").Chart.SeriesCollection(1).Points(x).Value
MsgBox (sem)
If sem > rr Then
rr = sem
xmax = x
End If

x = x + 1
Wend

'Add data label to the largest part of pie chart
ActiveSheet.ChartObjects("REJT").Activate
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).Points(xmax).Select
ActiveChart.SeriesCollection(1).Points(xmax).ApplyDataLabels  
1
  • Have you tried this Stackoverflow answer which is similar to your need. It will get you the Max value in the chart. You can just add to the code to assign the data label when it is found. Commented May 9, 2014 at 11:29

1 Answer 1

1

Here is one way to get the maximum point value from your pie chart and apply data label to its position:

Sub pieChartMaxDataLabel()
    Dim cht As Chart
    Set cht = ActiveChart

    maxValue = WorksheetFunction.Max(cht.SeriesCollection(1).Values)
    maxPosition = WorksheetFunction.Match(maxValue, cht.SeriesCollection(1).Values, 0)
    cht.SeriesCollection(1).Points(maxPosition).ApplyDataLabels

    Debug.Print "Max Value is: "; maxValue; " At Position: "; maxPosition
End Sub

Tested with chart values that look like this:

enter image description here

Results:

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.