Solved

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

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now