How to Change Pie Chart Slice Color Using VBA in Excel
Question details
The user needs to change the color of individual pie chart slices using a VBA script without using the Selection object.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating chart formatting and applying specific styles or theme colors to pie slices via a macro.
- Observed behavior
- Requires a VBA method to loop through existing charts and explicitly target specific data points within a series to apply a fill color.
Ensure that the Developer tab is enabled in your spreadsheet application and that your active worksheet contains at least one pie chart before running the macro.
Use VBA ChartObject to Format Pie Slices
Apply VBA code to loop through chart objects, target a specific series and point, and change the fill color directly without selecting the chart.
This method avoids the unreliable Selection object by explicitly declaring and setting ChartObject, Chart, Series, and Point variables. This approach ensures the formatting is applied consistently to the targeted pie slice.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor.
In the top menu, click on Insert and select Module to create a blank script window.
Copy and paste the code to declare your variables and apply the color. Example: Sub ChangePieSliceColor() Dim chtObj As ChartObject, cht As Chart, srs As Series, pt As Point; For Each chtObj In ActiveSheet.ChartObjects: Set cht = chtObj.Chart: Set srs = cht.SeriesCollection(1): Set pt = srs.Points(1): pt.Format.Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent4: Next chtObj: End Sub
Press F5 or click the Run button to execute the code. The first slice (Points 1) of every pie chart on the active sheet will change to the specified theme color.

Automate Chart Formatting with WPS Office
WPS Office provides robust VBA macro support, allowing you to execute scripts that automate chart styling, such as customizing pie chart slice colors, directly in your spreadsheet application.
- 1. Open WPS Spreadsheets: Launch WPS Office and open your macro-enabled workbook containing the pie chart.
- 2. Access the VBA Editor: Navigate to the Developer tab on the ribbon and click on the VBA Editor icon to start managing your scripts.
- 3. Run Your Macro: Insert your chart formatting module, paste your VBA code, and hit Run to immediately update your pie chart slice colors.

Frequently Asked Questions
How do I change the slice color to a specific RGB value instead of a theme color?
You can replace 'pt.Format.Fill.ForeColor.ObjectThemeColor = msoThemeColorAccent4' with 'pt.Format.Fill.ForeColor.RGB = RGB(255, 0, 0)' in your VBA code to apply a custom Red color.
Why isn't my VBA code affecting the pie chart?
Ensure the pie chart is located on the active worksheet when you run the macro. Additionally, verify that the index numbers used for 'SeriesCollection' and 'Points' match the actual data series structure of your chart.
Can I use this same VBA logic for other chart types like Bar or Column charts?
Yes, the same object hierarchy (Chart > SeriesCollection > Points) applies to most standard chart types, allowing you to format individual bars or columns similarly without relying on the Selection object.




