Apply color to Pie Chart slices in Excel

117 Views Asked by At

I want to create a pie chart in Excel with 33 pieces.

I want for each piece, depending on the value of the cell
green (No issue)
red (Need)
yellow (Opportunity)

For example,
Identifier # Results
1 No issue
2 Opportunity
3 Need
...
33

I created a regular pie chart but it is tedious to individually change the color on each slice.

I would like the color of the slice to be automated.

1

There are 1 best solutions below

0
Jon Peltier On

A pie chart may not be the best choice. It's not easy to label 33 slices clearly. Labeling in a horizontal bar chart will work much better, and the bar chart will probably take up less space and still be legible.

Pie and Bar Charts Conditionally Formatted by VBA

The code below works on either chart type.

Sub ColorByValue()
  If Not ActiveChart Is Nothing Then
    With ActiveChart
      Dim srs As Series
      Set srs = .SeriesCollection(1)
      Dim vals As Variant
      vals = srs.Values
      Dim iPt As Long
      For iPt = 1 To UBound(vals)
        Select Case vals(iPt)
          ' select your cutoff values and colors
          Case Is >= 8
            srs.Points(iPt).Format.Fill.ForeColor.RGB = 5287936 ' vbGreen
          Case Is <= 3
            srs.Points(iPt).Format.Fill.ForeColor.RGB = vbRed
          Case Else
            srs.Points(iPt).Format.Fill.ForeColor.RGB = 49407 ' vbYellow
        End Select
      Next
    End With
  Else
    MsgBox "Select a chart to format!", vbExclamation, "No Active Chart"
  End If
End Sub