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
Bookmarks