Results 1 to 1 of 1

VBA code to hide data labels on pie chart when value is zero

Threaded View

  1. #1
    Registered User
    Join Date
    02-20-2013
    Location
    Brisbane, Queensland
    MS-Off Ver
    Excel 2010
    Posts
    1

    VBA code to hide data labels on pie chart when value is zero

    Hi, I have several pie charts that I have written a macro using VBA to keep colours consistent across all charts and for data labels to appear on the charts.
    The problem I have is that data labels appear for ALL the legend even is the value is zero. How do I stop data labels appearing for data that has a value of zero?

    Sub ColorPieSlicesSector()
        ActiveChart.SetElement (msoElementDataLabelOutSideEnd)
        ActiveChart.ApplyDataLabels
        ActiveChart.SeriesCollection(1).DataLabels.Select
        Selection.ShowCategoryName = True
      ' This macro will re-color the pie slices in a chart
    ' So that slices for a specific category are similarly colored
    ' Select the chart before calling the macro
    '
    ' Find the number of pie slices in this chart
    'Reset chart first to 'style'
        ActiveChart.ClearToMatchStyle
        ActiveChart.ChartStyle = 42
        ActiveChart.ClearToMatchStyle
        NumPoints = ActiveChart.SeriesCollection(1).Points.Count
        ' Loop through each pie slice
        For X = 1 To NumPoints
            ' Save the label currently attached to this slice
            If ActiveChart.SeriesCollection(1). _
                Points(X).HasDataLabel = True Then
                    SavePtLabel = ActiveChart.SeriesCollection(1) _
                        .Points(X).DataLabel.Text
            Else
                SavePtLabel = ""
            End If
            ' Assign a new data label of just the point name
            ActiveChart.SeriesCollection(1).Points(X).ApplyDataLabels Type:= _
                xlDataLabelsShowLabel, AutoText:=True
            ThisPt = ActiveChart.SeriesCollection(1).Points(X).DataLabel.Text
            ' Based on the label of this slice, set the color
            Select Case ThisPt
                Case "Road"
                    ActiveChart.SeriesCollection(1). _
                        Points(X).Interior.ColorIndex = 43
                Case "Defence"
                    ActiveChart.SeriesCollection(1). _
                        Points(X).Interior.ColorIndex = 27
                Case "Rail"
                    ActiveChart.SeriesCollection(1). _
                        Points(X).Interior.ColorIndex = 45
                Case "Marine & Ports"
                    ActiveChart.SeriesCollection(1). _
                        Points(X).Interior.ColorIndex = 3
                Case "Water"
                    ActiveChart.SeriesCollection(1). _
                        Points(X).Interior.ColorIndex = 41
                Case "Resources"
                    ActiveChart.SeriesCollection(1). _
                        Points(X).Interior.ColorIndex = 39
                Case "CSG"
                    ActiveChart.SeriesCollection(1). _
                        Points(X).Interior.ColorIndex = 38
                Case "Road Maintenance"
                    ActiveChart.SeriesCollection(1). _
                        Points(X).Interior.ColorIndex = 24
            End Select
            ' Return the label to it's original pre-macro state
            ActiveChart.SeriesCollection(1). _
                Points(X).DataLabel.Text = SavePtLabel
        Next X
    End Sub

    Thanks
    Kathryn
    Last edited by jeffreybrown; 02-20-2013 at 11:36 PM. Reason: As per Forum Rule #3, please use code tags…Thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1