Solved

Highlight the max value of the Data Labels in all of the Excel Charts

Posted on 2014-04-20
2
516 Views
Last Modified: 2014-04-21
Dear Experts:

below macro deletes all data labels featuring 0% values from all the charts of the active worksheet. The macro works just fine. Could somebody help me re-write this code so that the additonal following requirements are met.

I would like to change the font color (to white) of the data labels in all Excel charts of the current worksheet to highlight the maximum values.

There is a tutorial on this on the web by:

http://peltiertech.com/WordPress/highlight-specific-data-label-in-excel-chart/

But I was not able to adjust that sample code to accommodate my requirements.

The code somebody will come up with hopefully can be a stand-alone code of course, it does not need to be incorporated in the code below.

Help is very much appreciated.  Thank you very much in advance.

Regards, andreas

Sub CleanUpChartLabels()
    Dim iPts As Integer
    Dim nPts As Integer
    Dim aVals As Variant
    Dim srs As Series
    Dim myChtObj As ChartObject
   
If msgbox("Would you like to delete 0% datalabels? ... " & vbCrLf & _
"... i.e. all Data labels featuring just 0% of the total count of " & ActiveSheet.ChartObjects.Count & " Charts will be deleted?", vbQuestion + vbYesNo, "0% Datalabels") = vbNo Then
Exit Sub
End If
 
For Each myChtObj In ActiveSheet.ChartObjects
  
   With myChtObj.Chart
        For Each srs In .SeriesCollection
            With srs
                If .HasDataLabels Then
                    nPts = .Points.Count
                   ' i = i + 1
                    aVals = .Values
                        For iPts = 1 To nPts
                            If aVals(iPts) = 0 Then
                                .Points(iPts).HasDataLabel = False
                            End If
                        Next
                End If
            End With
        Next
   End With
Next

End Sub

Open in new window

0
Comment
Question by:AndreasHermle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Accepted Solution

by:
Ejgil Hedegaard earned 500 total points
ID: 40011798
The Peltier code adjusted to highlight white, and handle all series in the charts.
As stand-alone.

Option Explicit

Sub HighlightMaxDataLabel()
    Dim srs As Series
    Dim vY As Variant
    Dim iPt As Long, nPts As Long
    Dim dMax As Double
    Dim iHighlightColor As Long
    Dim myChtObj As ChartObject

    ' highlight color: change to suit
    iHighlightColor = RGB(255, 255, 255)    'white

    For Each myChtObj In ActiveSheet.ChartObjects
        For Each srs In myChtObj.Chart.SeriesCollection
            
            ' reset all labels to original font color
            With srs.DataLabels.Font
                .Color = .Color
            End With
        
            vY = srs.Values
            nPts = srs.Points.Count

            ' find maximum value
            dMax = vY(1)
            For iPt = 2 To nPts
                If dMax < vY(iPt) Then
                    dMax = vY(iPt)
                End If
            Next iPt
    
            For iPt = 1 To nPts
                ' highlight all labels at maximum value
                If vY(iPt) = dMax Then
                    srs.Points(iPt).DataLabel.Font.Color = iHighlightColor
                End If
            Next iPt
        Next srs
    Next myChtObj
End Sub

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 40013261
Hi hgholt:

great this did the trick. Thank you very much for  your great and swift support.

Regards, Andreas
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question