Excel VBA Cell Color RGB: Set and Read Colors

VBA & macros · Updated November 19, 2026

In Excel VBA, set a cell’s fill color with Range("A1").Interior.Color = RGB(255, 0, 0); that example makes the cell red. Use .Font.Color instead to change text color, and read a cell’s color by assigning its .Interior.Color or .Font.Color value to a VBA variable.

What RGB means in Excel VBA

RGB stands for red, green, and blue, the three color components VBA combines to specify a color. The VBA RGB function takes three numbers in that order, each from 0 to 255: the red intensity, green intensity, and blue intensity. A component of 0 contributes none of that color; 255 is its maximum. Combining the components makes a broad range of colors.

For example, RGB(255, 0, 0) is red, RGB(0, 255, 0) is green, and RGB(0, 0, 255) is blue. Equal component values produce gray shades: RGB(128, 128, 128) is a medium gray, while RGB(255, 255, 255) is white and RGB(0, 0, 0) is black. Excel’s VBA color properties use a Long integer, and the RGB function returns the suitable Long value for the requested color.

This is different from typing a web-style hexadecimal color such as #FF0000 directly into VBA. The RGB equivalent is RGB(255, 0, 0). For an orange, for example, RGB(255, 165, 0) supplies the red, green, and blue components separately.

Set a cell’s fill color

A cell’s fill is controlled by its Interior object. The following procedure colors B2 on a worksheet named Sheet1. Change the worksheet name and address to match the workbook. Before running any macro, save a copy of the workbook: VBA actions generally cannot be reversed with Ctrl+Z.

Sub SetCellFillColor()
    Dim target As Range

    ' Change the sheet name and cell address as needed.
    Set target = ThisWorkbook.Worksheets("Sheet1").Range("B2")

    ' Apply a solid red fill.
    target.Interior.Color = RGB(255, 0, 0)
End Sub

To color a range, use a multi-cell address such as Range("B2:B10") or Range("B2:D10"). The same assignment applies the fill to every cell in the specified range. A useful example is a status cell: use RGB(0, 176, 80) for green, RGB(255, 192, 0) for amber, or RGB(255, 0, 0) for red. Choose values that remain distinguishable for the people who will read the sheet.

Read a cell’s color in VBA

To inspect a cell’s directly applied fill color, read its Interior.Color property. Store the result as a Long, the VBA integer type used for Excel color values. This example displays the value for B2 in a message box; it reports a number, not a color name.

Sub ShowCellFillColor()
    Dim target As Range
    Dim colorValue As Long

    ' Change the sheet name and cell address as needed.
    Set target = ThisWorkbook.Worksheets("Sheet1").Range("B2")
    colorValue = target.Interior.Color

    ' Display the numeric color value.
    MsgBox colorValue
End Sub

Reading the number is useful when you need to compare cells or reuse a color. For example, after storing one cell’s value, code can compare another cell’s Interior.Color with it. Two cells with the same color value can then be handled alike without checking each RGB component separately. Remember, though, that a numeric value is not a friendly label: if a macro needs to explain the result, it should map known values to names or report the individual red, green, and blue components.

Change text color, borders, and other formatting

Use the property that corresponds to the part of the cell you want to format. Interior.Color changes the fill, while Font.Color changes the text color. For example, Range("A1").Font.Color = RGB(0, 0, 255) makes the font blue without changing the fill. The distinction matters when a cell already has a background color: changing the font property does not replace it.

Borders have their own objects and properties. To set a border color, specify a border, such as Range("A1").Borders(xlEdgeBottom).Color = RGB(0, 0, 0). That colors the bottom edge; it does not fill the cell. In general, the object identifies what is being formatted and the Color property supplies its color.

Color is only one part of formatting. A fill also has a pattern, and a font has properties such as bold and size. If a cell does not appear as expected, check whether its fill pattern is visible and whether another formatting rule is affecting its displayed appearance.

RGB and ColorIndex are not the same

Excel VBA also has a ColorIndex property. Unlike Color, which represents a color value, ColorIndex refers to an entry in Excel’s color palette. It can be useful in older macros that were written around palette indexes, but it is less direct when you want to specify a particular red, green, and blue combination. For new code that needs a defined color, .Interior.Color = RGB(...) is usually clearer.

Do not confuse a palette index with an RGB component. Setting Interior.ColorIndex = 3 means “use palette index 3,” not “set the red component to 3.” Palette behavior can also make an old macro harder to interpret or maintain. When removing a fill, use a deliberate no-fill setting such as Interior.Pattern = xlNone; assigning a white fill is not equivalent, because white is still a fill and may cover formatting beneath it.

Conditional formatting and the displayed color

A common reason a color-reading macro seems wrong is conditional formatting. A cell can have one directly assigned fill while a conditional-formatting rule displays another fill when its condition is met. In that case, Range("A1").Interior.Color reads the cell’s ordinary formatting, not necessarily the color currently visible on screen.

In VBA, the DisplayFormat property can be used to inspect formatting as displayed, including the effect of conditional formatting. For example, a macro can read Range("A1").DisplayFormat.Interior.Color. This is useful for a procedure that needs to respond to the currently visible fill. It does not replace the conditional-formatting rule or change its logic; it reads the resulting display format.

When the goal is to color cells based on values, conditional formatting is often more suitable than a macro that repeatedly writes fixed fills. Rules update as values change and are managed through Excel’s conditional-formatting tools. A VBA macro is more appropriate when the coloring is part of a larger automated task or when a one-time formatting action is needed. If a macro is intended to interpret a condition, checking the cell’s value or the rule’s logic may be more reliable than using color as the only signal.

Choose and check RGB values

VBA’s RGB function expects three component numbers, not a single decimal color code or a string. A call such as RGB(30, 144, 255) produces a bright blue shade. If you already have a color in another format, convert it to red, green, and blue components before passing it to the function. A common source of mistakes is reversing the order: VBA uses red first, green second, and blue third.

For a quick visual check, assign a test color to a spare cell and compare it with the intended shade. A worksheet palette, design specification, or existing cell can provide a reference. You can also read the color value from that existing cell and reuse it elsewhere, rather than guessing component values. Keep in mind that a monitor, theme, or conditional-formatting rule can affect what a reader sees; the numeric color and its appearance are related but not always sufficient to explain the whole formatting result.

  • Use three values from 0 to 255 in red-green-blue order.
  • Use .Interior.Color for fills and .Font.Color for text.
  • Use .DisplayFormat.Interior.Color when you need the displayed fill affected by conditional formatting.
  • Use ColorIndex only when working with a palette index is specifically useful.

Common problems and how to fix them

If the macro runs but the cell does not look colored, verify that it targets the right workbook, worksheet, and cell. ThisWorkbook means the workbook containing the VBA code; ActiveWorkbook means the workbook currently active in Excel. Those can differ. A misspelled sheet name or an unexpected active workbook can make code affect a different place than intended or cause a runtime error.

If a color appears to change and then revert, check for conditional formatting, another macro, or code that runs later and overwrites the format. If the fill looks unchanged, verify that the cell is not being viewed through a conditional-formatting result and that the intended property is being changed. A font color assignment will not change the fill, and changing ColorIndex is not the same as assigning an RGB color.

If the VBA editor reports a compile or runtime error, check that the code is in a standard module or an appropriate worksheet or workbook module, and that the worksheet reference is valid. A macro-enabled workbook format such as .xlsm is needed to preserve VBA code. Excel for the web can open macro-enabled files but cannot create, run, or edit VBA macros; use the desktop Excel application for these procedures.

Running the macro safely

On Excel for Microsoft 365 for Windows, open the VBA editor with Alt+F11, insert a standard module through Insert > Module, and paste the procedure there. Run it from the editor with the cursor inside the procedure and press F5, or return to Excel and use Developer > Macros to select and run it. If the Developer tab is missing, enable it through File > Options > Customize Ribbon and select Developer.

Excel for Mac supports VBA and the RGB color properties described here, although keyboard shortcuts and some dialog locations differ. The VBA editor can be opened with Option+F11 on many Mac keyboards; depending on the keyboard’s function-key settings, you may need Fn as well. Use Excel’s Developer tools to run the macro if that shortcut differs on your setup. Do not expect VBA to run in Excel for the web. Save a separate copy before running any macro that changes formatting, because Ctrl+Z (or Command+Z on Mac) does not undo VBA changes.

Frequently Asked Questions

What is the VBA code for a red cell?
Use Range("A1").Interior.Color = RGB(255, 0, 0) to give A1 a red fill. Replace A1 with the cell address you want to format.

How do I make the cell text a different color?
Use the cell’s Font.Color property instead of Interior.Color, for example Range("A1").Font.Color = RGB(0, 0, 255) for blue text.

How can I get the RGB color from a cell?
Read Range("A1").Interior.Color into a Long variable for the directly applied fill color. Use Range("A1").DisplayFormat.Interior.Color if you need the currently displayed fill, including conditional formatting.

Why does the color value look like a large number?
The Color property returns a Long integer that encodes the color; it does not return a word such as “red” or a string such as #FF0000. Use RGB(red, green, blue) to create a value from readable component numbers.

Can I run an Excel VBA color macro in a browser?
No. Excel for the web does not run VBA macros. Open the workbook in desktop Excel for Windows or Mac to run VBA; save it in a macro-enabled format if you need to retain the code.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.