Use VBA to set color in pie charts

Tcurtis

Board Regular
Joined
Aug 15, 2009
Messages
149
Is there a way to set the colors in a pie chart without having to manipulate them everytime I run a report graph? I have 6 different categories that are counted and I want each category to not vary from month to month in color.
Lost = Red
Found = Green
Obsolete = Yellow, etc.
Can I assign colors to a table or can I use VBA in the chart to acheive this?

Thanks
 
There is probably a better way, but courtesy of the brute force method:

Code:
Option Explicit

Sub SetPieChartColorsBySliceName()

    Dim lX As Long
    Dim arySeriesData, aryPointsNameLocation, aryPointNames
    Dim namesWorksheet As String
    Dim namesRange As String
    
    With ActiveSheet.ChartObjects(1).Chart
        
        If .Type <> 5 Then
            MsgBox "The first chart on this worksheet is not a Pie Chart."
            GoTo End_Sub
        End If
        
        arySeriesData = Split(ActiveSheet.ChartObjects(1).Chart.SeriesCollection(1).formula, ",")
        aryPointsNameLocation = Split(arySeriesData(1), "!")
        namesWorksheet = aryPointsNameLocation(0)
        namesRange = aryPointsNameLocation(1)
        aryPointNames = Worksheets(namesWorksheet).Range(namesRange)
        
        For lX = LBound(aryPointNames, 1) To UBound(aryPointNames, 1)
            .SeriesCollection(1).Points(lX).Select
            
            Select Case aryPointNames(lX, 1)
            Case "Lost"
                Selection.Format.Fill.ForeColor.RGB = RGB(255, 100, 0)
            Case "Found"
                Selection.Format.Fill.ForeColor.RGB = RGB(100, 255, 0)
            Case "Obsolete"
                Selection.Format.Fill.ForeColor.RGB = RGB(100, 0, 255)
            Case Else
                Selection.Format.Fill.ForeColor.RGB = RGB(0, 0, 0)
            End Select
            
        Next

    End With

End_Sub:

End Sub
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top