Mastering Pattern Color Index in VBA: A Comprehensive Guide
Pattern Color Index (PCID) is a crucial aspect of working with charts in Microsoft Excel's Visual Basic for Applications (VBA). It allows you to manipulate the color of pattern fills in charts, adding a layer of customization to your visualizations. In this guide, we'll delve into the world of PCID, providing you with a solid understanding of how to use it and offering practical examples to enhance your VBA skills.
Understanding Pattern Color Index
PCID is a numeric value that represents a specific color from a predefined set of colors in Excel. These colors are part of the Office theme and are consistent across different versions of Excel. The PCID values range from 1 to 56, with each number corresponding to a unique color.
To understand PCID better, let's look at a few examples:

- PCID 1 corresponds to the color Automatic.
- PCID 2 is associated with the color No Fill (or No Color).
- PCID 3 represents the color Black.
- PCID 56 is linked to the color White.
Accessing Pattern Color Index in VBA
To work with PCID in VBA, you'll first need to understand how to access it. The `ColorIndex` property of the `ChartFillFormat` object is used to set or return the PCID of a chart element.
Here's a simple example of how to access the PCID of a chart's plot area:
```vba Sub AccessPCID() Dim cht As Chart Set cht = ActiveSheet.ChartObjects(1).Chart Debug.Print "Plot area color index: " & cht.PlotArea.Fill.ColorIndex End Sub ```
Setting Pattern Color Index in VBA
Now that you know how to access the PCID let's explore how to set it. To change the color of a chart element, you simply need to assign a new PCID value to the `ColorIndex` property.

Let's modify the previous example to change the plot area's color:
```vba Sub SetPCID() Dim cht As Chart Set cht = ActiveSheet.ChartObjects(1).Chart ' Change plot area color to blue (PCID 41) cht.PlotArea.Fill.ColorIndex = 41 ' Change series color to red (PCID 3) cht.SeriesCollection(1).MarkerStyle = xlMarkerStyleNone cht.SeriesCollection(1).MarkerBackgroundColorIndex = 3 End Sub ```
Working with PCID and Custom Colors
While PCID provides a predefined set of colors, you might want to use custom colors in your charts. To do this, you can use the `RGB` function in VBA, which returns a long integer representing a specific color.
Here's an example of how to set a custom color using RGB and PCID:

```vba Sub CustomColorUsingPCID() Dim cht As Chart Set cht = ActiveSheet.ChartObjects(1).Chart ' Define a custom color using RGB Dim customColor As Long customColor = RGB(255, 128, 0) ' Orange ' Change plot area color to the custom color cht.PlotArea.Fill.ColorIndex = customColor End Sub ```
PCID and Chart Element Types
PCID can be applied to various chart elements, including plot area, series, data labels, and more. Here's a table summarizing the applicable chart elements and their corresponding properties:
| Chart Element | Property |
|---|---|
| Plot Area | `Chart.PlotArea.Fill.ColorIndex` |
| Series | `Chart.SeriesCollection(index).MarkerBackgroundColorIndex` |
| Data Labels | `Chart.SeriesCollection(index).DataLabels.Font.ColorIndex` |
| Axes | `Chart.Axes(index).TickLabels.Font.ColorIndex` |
| Legend | `Chart.Legend.Font.ColorIndex` |
With this comprehensive guide, you're now equipped to harness the power of Pattern Color Index in VBA, elevating your chart customization skills to new heights. Happy coding!






















