Solved

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

Posted on 2014-04-20
2
500 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
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

697 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