The VBA ColorIndex property in Excel sets fill colors, border colors and font colors. Excel VBA ColorIndex returns index values from 1 to 56, plus -4105 and -4142. Use enumeration -4105 (xlColorIndexAutomatic) for default colors, and -4142 (xlColorIndexNone) to clear colors or set no color.
Syntax of Excel VBA ColorIndex
You can set or return the color index value of Excel objects using the following syntax.
expression.ColorIndex
To get the ColorIndex value of an Excel font, interior or border color and store it in a variable:
dblColorValue = expression.ColorIndex
To set the ColorIndex value on an Excel color object:
expression.ColorIndex = IndexValue ' 1 to 56, -4105 or -4142
ColorIndex in Excel VBA
Here are all 56 Excel VBA ColorIndex values with their corresponding colors and RGB equivalents.
| ColorIndex | Excel VBA color | ColorIndex | Excel VBA color |
|---|---|---|---|
| 1 | RGB(0,0,0) | 29 | RGB(128,0,128) |
| 2 | RGB(255,255,255) | 30 | RGB(128,0,0) |
| 3 | RGB(255,0,0) | 31 | RGB(0,128,128) |
| 4 | RGB(0,255,0) | 32 | RGB(0,0,255) |
| 5 | RGB(0,0,255) | 33 | RGB(0,204,255) |
| 6 | RGB(255,255,0) | 34 | RGB(204,255,255) |
| 7 | RGB(255,0,255) | 35 | RGB(204,255,204) |
| 8 | RGB(0,255,255) | 36 | RGB(255,255,153) |
| 9 | RGB(128,0,0) | 37 | RGB(153,204,255) |
| 10 | RGB(0,128,0) | 38 | RGB(255,153,204) |
| 11 | RGB(0,0,128) | 39 | RGB(204,153,255) |
| 12 | RGB(128,128,0) | 40 | RGB(255,204,153) |
| 13 | RGB(128,0,128) | 41 | RGB(51,102,255) |
| 14 | RGB(0,128,128) | 42 | RGB(51,204,204) |
| 15 | RGB(192,192,192) | 43 | RGB(153,204,0) |
| 16 | RGB(128,128,128) | 44 | RGB(255,204,0) |
| 17 | RGB(153,153,255) | 45 | RGB(255,153,0) |
| 18 | RGB(153,51,102) | 46 | RGB(255,102,0) |
| 19 | RGB(255,255,204) | 47 | RGB(102,102,153) |
| 20 | RGB(204,255,255) | 48 | RGB(150,150,150) |
| 21 | RGB(102,0,102) | 49 | RGB(0,51,102) |
| 22 | RGB(255,128,128) | 50 | RGB(51,153,102) |
| 23 | RGB(0,102,204) | 51 | RGB(0,51,0) |
| 24 | RGB(204,204,255) | 52 | RGB(51,51,0) |
| 25 | RGB(0,0,128) | 53 | RGB(153,51,0) |
| 26 | RGB(255,0,255) | 54 | RGB(153,51,102) |
| 27 | RGB(255,255,0) | 55 | RGB(51,51,153) |
| 28 | RGB(0,255,255) | 56 | RGB(51,51,51) |
Get all 56 colors in a workbook
The free example file below prints this palette into Excel as real fills, so you can see the colors as Excel renders them rather than as hex codes on a page. It also converts any hex code to RGB and finds the closest ColorIndex.
ColorIndex 1–56 only covers the legacy palette
In Excel, the ColorIndex property refers to a palette of 56 specific, predefined colors numbered from 1 to 56. This system is primarily used in VBA and in custom number formatting.
The 56-color index dates from Excel 2003 and earlier. For anything outside it, use .Color with an RGB value instead — see RGB colors in Excel VBA below.
VBA to print the ColorIndex table
This macro prints all 56 ColorIndex values and their colors into an Excel sheet, so you can see the palette in your own workbook.
Sub sbExcel_VBA_PrintColorIndex()
rowCntr = 2
colCntr = 2
For iCntr = 1 To 56
Cells(rowCntr, colCntr).Interior.ColorIndex = iCntr
Cells(rowCntr, colCntr) = iCntr
If iCntr > 1 And iCntr Mod 14 = 0 Then
colCntr = colCntr + 1
rowCntr = 2
Else
rowCntr = rowCntr + 1
End If
Next
End Sub
Set ColorIndex in Excel VBA
These macros set the ColorIndex on a range of cells in an Excel worksheet.
Font colors in Excel VBA
Set font colors using the ColorIndex property of the Font object. This Excel VBA font color macro sets the font color of the range A1:E20.
Sub SetFontColorIndex_Range()
Range("A1:E20").Font.ColorIndex = 40
End Sub
You can also read the font color into a variable:
myVar = Range("A1").Font.ColorIndex
Interior colors in Excel VBA
Change the interior or fill color of a range using the ColorIndex property. This Excel interior color macro changes the fill color of an object.
Sub SetInteriorColorIndex_Range()
Range("A1:E20").Interior.ColorIndex = 41
End Sub
To get a cell color in Excel VBA, read the interior ColorIndex back:
myVar = Range("A1:E20").Interior.ColorIndex
Border colors in Excel VBA
The ColorIndex property of the Borders object sets border colors.
Sub SetBordersColorIndex_Range()
Range("A1:E20").Borders.ColorIndex = 42
End Sub
Get cell color in Excel VBA
Reading a color back is the mirror of setting one. The property you read determines what you get: ColorIndex returns a number from 1 to 56, while Color returns a long integer holding the full RGB value.
Sub GetCellColor()
Dim idx As Long, clr As Long
idx = Range("A1").Interior.ColorIndex ' 1 to 56, or -4142 if no fill
clr = Range("A1").Interior.Color ' long integer, e.g. 255 for red
MsgBox "ColorIndex: " & idx & vbNewLine & "Color: " & clr
End Sub
Get the red, green and blue values of a cell
The Color property returns a single long integer, not three separate numbers. VBA stores it as BGR rather than RGB, so extract the channels like this:
Sub GetCellRGB()
Dim clr As Long, R As Integer, G As Integer, B As Integer
clr = Range("A1").Interior.Color
R = clr Mod 256
G = (clr \ 256) Mod 256
B = (clr \ 65536) Mod 256
MsgBox "R=" & R & " G=" & G & " B=" & B
End Sub
Why the channels look reversed
Excel stores colors as &HBBGGRR, so the lowest byte is red and the highest is blue — the opposite order to the RGB() function’s arguments. This catches almost everyone the first time they read a color back.
Get the cell color of a range
Reading ColorIndex from a multi-cell range returns a value only if every cell shares the same color. If they differ you get Null, so test for it before using the result.
Sub GetRangeColor()
Dim v As Variant
v = Range("A1:E20").Interior.ColorIndex
If IsNull(v) Then
MsgBox "Mixed colors in the range"
Else
MsgBox "All cells use ColorIndex " & v
End If
End Sub
Count cells by color
Excel has no built-in function for this, which is why it is one of the most common reasons people reach for VBA. Use it in a worksheet as =CountByColor(A1:E20, G1).
Function CountByColor(rng As Range, refCell As Range) As Long
Dim c As Range, n As Long
For Each c In rng
If c.Interior.Color = refCell.Interior.Color Then n = n + 1
Next c
CountByColor = n
End Function
Color changes do not trigger a recalculation
Changing a fill color is not a value change, so Excel will not re-run the function. Press Ctrl + Alt + F9 to force a full recalculation. This approach also ignores colors applied by conditional formatting — those live in DisplayFormat.Interior.Color, which is unavailable inside a user-defined function.
Clear colors in Excel VBA
Sometimes you need no fill at all. These macros clear font, border and fill colors, setting them back to automatic or none.
Clear background color in Excel VBA
Use this macro to clear the background color and set no interior fill.
Sub SetClearBackgroundColor_ColorIndex_Range()
Range("A1:E20").Interior.ColorIndex = xlNone
'OR
'Range("A1:E20").Interior.ColorIndex = -4142
End Sub
Interior.ColorIndex = -4142 is the enumeration behind xlNone, which clears the background or fill color.
Border colors clear the same way:
Sub SetClearBorders_ColorIndex_Range()
Range("A1:E20").Borders.ColorIndex = -4142
End Sub
Font colors reset to automatic with -4105 rather than -4142:
Sub SetClearFontColorIndex_Range()
Range("A1:E20").Font.ColorIndex = -4105
End Sub
| Enumeration | Constant | What it does |
|---|---|---|
-4105 |
xlColorIndexAutomatic |
Resets to the default color. Use for fonts. |
-4142 |
xlColorIndexNone |
Removes the color entirely. Use for interiors and borders. |
VBA colors
There are three ways to set colors in VBA: the ColorIndex property, VBA color constants, and RGB values. ColorIndex is covered above — here are the other two.
Excel VBA color constants
The built-in color constants are the quickest option when one of the eight basic colors will do.
Sub sbExcel_VBA_ColorConstants()
Cells(2, 4).Interior.Color = vbBlack
Cells(3, 4).Interior.Color = vbRed
Cells(4, 4).Interior.Color = vbGreen
Cells(5, 4).Interior.Color = vbYellow
Cells(6, 4).Interior.Color = vbBlue
Cells(7, 4).Interior.Color = vbMagenta
Cells(8, 4).Interior.Color = vbCyan
Cells(9, 4).Interior.Color = vbWhite
End Sub
| VBA color constant | Value | Color and RGB |
|---|---|---|
vbBlack |
0x0 | RGB(0,0,0) |
vbRed |
0xFF | RGB(255,0,0) |
vbGreen |
0xFF00 | RGB(0,255,0) |
vbYellow |
0xFFFF | RGB(255,255,0) |
vbBlue |
0xFF0000 | RGB(0,0,255) |
vbMagenta |
0xFF00FF | RGB(255,0,255) |
vbCyan |
0xFFFF00 | RGB(0,255,255) |
vbWhite |
0xFFFFFF | RGB(255,255,255) |
RGB colors in Excel VBA
Constants and ColorIndex give you a limited palette. RGB lets you use any combination of red, green and blue — 0 to 255 on each channel, so roughly 16.7 million colors.
Sub ChangeBackgroundColorRGB_Range()
Range("A1:E20").Interior.Color = RGB(125, 205, 99)
End Sub
Note that RGB values are assigned to .Color, not .ColorIndex. Mixing the two is the most common source of unexpected results when setting colors in VBA.
Hex color codes in Excel VBA
VBA has no direct hex color input, so a hex code from a brand guide or a design tool has to be converted. There are two ways.
Using the RGB function
Split the hex into its three pairs and pass them as decimals. #7DCD63 becomes 7D, CD, 63 — that is 125, 205, 99.
Range("A1").Interior.Color = RGB(125, 205, 99)
Using a hex literal directly
VBA accepts hex with the &H prefix, but the byte order is reversed — BGR, not RGB. So #7DCD63 is written as &H63CD7D.
Range("A1").Interior.Color = &H63CD7D
Convert any hex code with a function
This function handles the reversal for you, so you can paste hex codes straight from a design spec.
Function HexToColor(hexCode As String) As Long
Dim h As String
h = Replace(hexCode, "#", "")
HexToColor = RGB(CLng("&H" & Mid(h, 1, 2)), _
CLng("&H" & Mid(h, 3, 2)), _
CLng("&H" & Mid(h, 5, 2)))
End Function
Sub UseHexColor()
Range("A1").Interior.Color = HexToColor("#7DCD63")
End Sub
| You have | Write it as | Note |
|---|---|---|
Hex #7DCD63 |
RGB(125, 205, 99) |
Clearest to read |
Hex #7DCD63 |
&H63CD7D |
Byte order reversed |
| ColorIndex 1–56 | .ColorIndex = 40 |
Legacy palette only |
| Named color | vbGreen |
Eight constants only |
Free download: ColorIndex example workbook
One macro-enabled workbook containing the palette, a hex converter and every macro from this page, ready to run. Free, no sign-up.
| Sheet | What it does |
|---|---|
| Palette | All 56 ColorIndex values as real Excel fills, with R, G, B channels, the hex code and the &H literal already byte-reversed. |
| Converter | Type a hex code and get the RGB() call, the &H literal, and the three closest ColorIndex values ranked by distance. |
| ReadMe | A legend for both sheets and a list of every macro in the file. |
Macros included
The module is called ColorIndexExamples. Press Alt + F11 to open it, click inside any procedure and press F5 to run.
| Group | Procedures |
|---|---|
| Print the palette | PrintColorIndexPalette, PrintColorIndexReference |
| Set colors | SetFontColorIndex, SetInteriorColorIndex, SetBordersColorIndex, SetInteriorRGB, SetColorConstants |
| Clear colors | ClearInteriorColor, ClearBorderColor, ClearFontColor |
| Read colors | GetCellColor, GetCellRGB, GetRangeColor, GetDisplayedColor |
| Convert hex | HexToColor, ColorToHex |
| Worksheet functions | =CountByColor(A1:E20, G1), =SumByColor(A1:E20, G1), =CellHex(A1) |
Enabling the macros
The macros are unsigned, so Excel shows a yellow security bar the first time you open the file — click Enable Content to run them. If the file came through email or a download folder Windows has marked as blocked, right-click it, choose Properties and tick Unblock first. Every line of code in the workbook is published on this page, so you can read it before you run it.
Frequently asked questions
What is ColorIndex in Excel VBA?
ColorIndex is a property that sets or returns a color using a number from 1 to 56, drawn from Excel’s legacy color palette. It applies to the Font, Interior and Borders objects. Use -4105 for automatic and -4142 for no color.
What is the difference between Color and ColorIndex?
ColorIndex takes a number from the fixed 56-color palette. Color takes a full RGB value and gives you access to all 16.7 million colors. Use Color unless you specifically need a palette index.
How do I get the color of a cell in VBA?
Read Range("A1").Interior.Color for the RGB value as a long integer, or Range("A1").Interior.ColorIndex for the palette number. On a multi-cell range both return Null if the cells do not all share one color.
How do I use a hex color code in VBA?
Either convert it to decimals and use RGB(125, 205, 99), or write it as a hex literal with the byte order reversed — #7DCD63 becomes &H63CD7D.
How do I remove a cell’s fill color in VBA?
Set Interior.ColorIndex = -4142, which is xlColorIndexNone. For fonts use -4105 (xlColorIndexAutomatic) instead — that resets to the default rather than removing the color.
Why does my VBA color look different from the hex code I entered?
Almost always the byte order. Excel stores colors as &HBBGGRR, so a hex literal has to be written blue-green-red rather than red-green-blue. Use the RGB() function if you want to avoid thinking about it.
Can VBA read a color applied by conditional formatting?
Yes, but not through Interior.Color — that returns the underlying fill. Use Range("A1").DisplayFormat.Interior.Color instead. Note that DisplayFormat is unavailable inside a user-defined function.
Is there a ColorIndex example file I can download?
Yes. The free macro-enabled workbook above prints all 56 colors into Excel as real fills, converts any hex code to RGB and the &H literal, finds the closest ColorIndex, and contains every macro from this page ready to run.
How many colors does ColorIndex support?
Fifty-six, numbered 1 to 56, plus -4105 and -4142. The palette dates from Excel 2003. Anything beyond it needs the Color property with an RGB value.
Related Excel VBA guides
- 100+ most useful Excel VBA codes — the full macro library.
- VBA Code Explorer — browse by object, method and property.
- Excel chart VBA examples — including chart series colors.
- 100+ VBA interview questions — with worked answers.
- Excel VBA tutorial — from basics to advanced programming.





