In Excel VBA, the most reliable way to set a cell’s color is with the RGB(red, green, blue) function, such as Range("A1").Interior.Color = RGB(255, 0, 0) for red. VBA also supports hexadecimal-style long integer color values and the older ColorIndex palette, but RGB is usually clearer and more portable.
How VBA represents colors
Excel stores a color as a number. The RGB function builds that number from three channels: red, green, and blue. Each channel takes a whole-number value from 0 through 255. Mixing the channels creates other colors: equal values make shades of gray, while combining full red, green, and blue produces white.
RGB(0, 0, 0)is black.RGB(255, 255, 255)is white.RGB(255, 0, 0)is red.RGB(0, 128, 0)is a dark green.RGB(0, 0, 255)is blue.RGB(255, 255, 0)is yellow.
Excel’s VBA editor uses the function name RGB even though the interface may display the spelling “Color.” The color property you assign determines which part of a cell is affected: Interior.Color sets the fill, while Font.Color sets the text color.
Set a fill, font, or border color
To run a short example, open the Visual Basic Editor with Alt+F11 on Windows, choose Insert > Module, and paste the macro into the module. Run it from the editor or from Excel’s Developer > Macros command. On Mac, use Option+F11 or Fn+Option+F11 depending on the keyboard, or open the editor through Tools > Macro > Visual Basic Editor.
This example colors a cell’s fill, text, and bottom border separately:
Sub ApplyExampleColors()
' Color A1 with a blue fill and white text.
With ThisWorkbook.Worksheets("Sheet1").Range("A1")
.Interior.Color = RGB(0, 112, 192)
.Font.Color = RGB(255, 255, 255)
.Borders(xlEdgeBottom).Color = RGB(0, 0, 0)
End With
End Sub
Change "Sheet1" to the actual worksheet name. The border example targets only the bottom edge; other constants include xlEdgeTop, xlEdgeLeft, and xlEdgeRight. A cell can therefore have different colors for its fill, font, and borders.
Before running any macro, save a copy of the workbook. VBA changes generally cannot be reversed with Ctrl+Z; on Mac, Command+Z is likewise not a dependable way to undo macro actions. Save macro-enabled workbooks as .xlsm if you need to retain VBA code.
RGB values and hexadecimal color codes
Design tools and web pages often provide colors as hexadecimal codes, such as #0070C0. That is not the same format as a VBA RGB argument, and pasting the string directly into Interior.Color will not work. Convert the six hex digits into three decimal channel values: 00 is red, 70 is green, and C0 is blue. In decimal, those are 0, 112, and 192, so the VBA equivalent is RGB(0, 112, 192).
VBA can also express a color as a hexadecimal number, but the byte order can be confusing: the numeric value used by VBA commonly appears as &HBBGGRR, with blue first and red last. For example, the RGB color RGB(0, 112, 192) corresponds to &HC07000 in that representation. Prefer RGB when writing or maintaining code because its channel order matches the familiar red-green-blue convention.
ColorIndex versus Color
ColorIndex is an older way to select from Excel’s indexed color palette. For example, Range("A1").Interior.ColorIndex = 3 usually selects red in the standard palette. But an index identifies a palette slot, not a specific RGB color. The actual appearance can depend on the workbook’s color palette, so an index may display differently in another workbook or after palette changes.
Use .Color with RGB when you need a particular shade to remain consistent. Use .ColorIndex when working with older VBA projects that depend on palette indexes or when the indexed palette is intentional. The special value xlColorIndexAutomatic can be used where Excel supports automatic colors; xlColorIndexNone indicates no color for properties that allow it.
Read a color or return it to automatic
You can inspect a cell’s current fill color through its Interior.Color property. The value is a number, so it may not look like a familiar RGB triplet when shown in the Immediate window. To retrieve its red, green, and blue components, use VBA’s Red, Green, and Blue functions on the color value:
Sub ShowCellColor()
Dim colorValue As Long
Dim target As Range
' Read the fill color from A1.
Set target = ThisWorkbook.Worksheets("Sheet1").Range("A1")
colorValue = target.Interior.Color
Debug.Print Red(colorValue), Green(colorValue), Blue(colorValue)
End Sub
To remove a manually applied fill, use Range("A1").Interior.Pattern = xlNone. This removes the fill pattern rather than painting the cell white; that distinction matters if the worksheet background or formatting should show through. To restore the font’s automatic color, use Range("A1").Font.ColorIndex = xlColorIndexAutomatic.
Common problems and conditional formatting
- The wrong shade appears: Confirm that the values are in red, green, blue order and each is between 0 and 255. A frequent mistake is reversing red and blue when converting from a hexadecimal value.
- The font changes but the cell does not: Check that you used
Font.Colorfor text orInterior.Colorfor the fill. - Your fill assignment appears ignored: Conditional formatting may be overriding the visible appearance. A direct VBA format change does not necessarily remove a conditional-formatting rule. Review the rules through Home > Conditional Formatting > Manage Rules.
- The code errors on a sheet name: Make sure the worksheet name in the code matches the tab name exactly, including spaces.
If a color should change automatically when cell values change, conditional formatting is often a better choice than a macro. On Windows, use Home > Conditional Formatting to create rules, or use VBA to create a conditional-formatting rule when setting up a workbook. A macro that paints cells once does not automatically recalculate its colors when values later change unless the macro is run again or an appropriate worksheet event is used.
Frequently Asked Questions
What is the VBA color code for red?
Use RGB(255, 0, 0) for standard red. Assign it to the property you want to change, such as Range("A1").Interior.Color for a fill or Range("A1").Font.Color for text.
How do I use a hex color in Excel VBA?
Convert the hex code’s red, green, and blue pairs into decimal values, then pass them to RGB. For #0070C0, the channels are 0, 112, and 192, so use RGB(0, 112, 192).
What is the difference between Color and ColorIndex?Color stores a specific color value and works naturally with RGB. ColorIndex refers to an indexed palette slot, whose displayed color can depend on the workbook’s palette.
Can VBA color a range instead of one cell?
Yes. Use a range such as Range("A1:A10").Interior.Color = RGB(255, 255, 0) to apply the same fill to all cells in that range. For separate colors based on each row’s value, use a loop, conditional formatting, or a worksheet event as appropriate.
Why does my VBA color not match the color shown in Excel?
Conditional formatting may be controlling the visible color, or the RGB channels may have been entered in the wrong order. Also check whether you are changing the fill, font, or border property you intended.
Related
- Create QR Codes in Word Mail Merge
- How to Highlight Zip Codes in Excel
- Excel Color Codes: How to Apply, Save, and Reuse Them
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Range to String Array: 2 Reliable MethodsThe most direct way to convert a VBA range to a string array is to read the range’s…
- Excel VBA UserForm Code Examples and SetupExcel VBA UserForms are custom dialog boxes for collecting information or guiding a task; useful examples include opening…
- Excel VBA vs Power Query: Key Differences and When to Use EachUse Power Query when the main job is importing, cleaning, combining, and reshaping data that you will refresh;…
- Excel VBA: Open a Workbook Without Updating LinksTo open an Excel workbook with VBA and skip updating its external workbook links, pass UpdateLinks:=0 to Workbooks.Open.…
- VBA InputBox With a Drop-Down List: Your OptionsNo: VBA’s built-in InputBox does not include a drop-down list, and Application.InputBox does not add one. To let…
- Excel VBA Copy Paste Transpose: Working Code and TipsTo copy a range and paste it transposed with Excel VBA, copy the source range and use PasteSpecial…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.