logo
search
VBA & Macro Problems

How to Change Pie Chart Slice Color Using VBA in Excel

WPS EditorWPS Editor Sep 28, 2026 869 views

Question details

The user needs to change the color of individual pie chart slices using a VBA script without using the Selection object.

How to Change a Pie Chart Slice Color with VBA Code
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) Editor.

2
Insert a New Module

In the top menu, click on Insert and select Module to create a blank script window.

3
Enter the VBA Code

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

4
Run the Macro

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.

Use VBA ChartObject to Format Pie Slices
Targeting Different Slices: You can change 'Points(1)' to another index number (e.g., 'Points(2)') to target different slices of your pie chart.
WPS Spreadsheet Macros

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. 1. Open WPS Spreadsheets: Launch WPS Office and open your macro-enabled workbook containing the pie chart.
  2. 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. 3. Run Your Macro: Insert your chart formatting module, paste your VBA code, and hit Run to immediately update your pie chart slice colors.
Highly compatible with Microsoft Excel .xlsm formats and VBA scripts.Easily automate data visualization and advanced chart formatting tasks.Free and lightweight Office alternative with a familiar user interface.
microsoft office alternative - wps office

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.