Excel VBA Cell Color: Read, Set, and Fix Common Issues

VBA & macros · Updated November 19, 2026

In Excel VBA, set a cell’s fill color with Range("A1").Interior.Color = RGB(255, 230, 153), and read it with Range("A1").Interior.Color. Use Interior.Color for a cell’s ordinary fill; if conditional formatting determines the visible color, read DisplayFormat.Interior.Color instead.

What Excel VBA Means by Cell Color

A cell’s background fill is an Interior property in Excel’s object model. In VBA, the Interior object exposes properties such as Color, ColorIndex, Pattern, and theme-related settings. For most macros, Interior.Color is the clearest choice because it represents the fill as a color value rather than as an entry in a palette.

Cell fill is not the same as font color, borders, or the color of a chart or shape. For text color, use the cell’s Font.Color property. A border has its own color property, such as Borders(xlEdgeBottom).Color. Identifying which part of the cell you mean prevents a macro from changing the wrong formatting.

VBA’s RGB function creates a color value from red, green, and blue components. Each component is an integer from 0 through 255. For example, RGB(255, 0, 0) is red, RGB(0, 0, 0) is black, and RGB(255, 255, 255) is white. The components make it practical to specify a color directly without looking up a palette index.

Set a Cell or Range to a Specific Fill Color

To change a fill with a macro, qualify the range with its worksheet so Excel knows exactly which cells to format. This example colors cell B2 on Sheet1 a pale yellow. Put the procedure in a standard VBA module, replacing the sheet name or range as needed:

Sub SetCellFillColor()
    Dim target As Range

    ' Point to the cell to format.
    Set target = ThisWorkbook.Worksheets("Sheet1").Range("B2")

    ' Apply a pale-yellow fill.
    target.Interior.Color = RGB(255, 230, 153)
End Sub

The same assignment works with a multi-cell range. For example, change the final line to target.Interior.Color = RGB(255, 230, 153) after setting target to Range("B2:B10") to apply the color to every cell in that range. The macro sets the fill of each cell; it does not change the font or border.

Before running a macro that changes a workbook, save a copy. VBA changes cannot be reversed with Ctrl+Z, and a macro may format more cells than intended if its range reference is wrong. On Windows, open the Visual Basic Editor with Alt+F11; on a Mac, use Option+F11 (some keyboards also require Fn), or use Developer > Visual Basic. Save a macro-enabled workbook as an .xlsm file if you need to keep its VBA code.

Read and Compare a Cell’s Fill Color

To inspect a cell’s direct fill, assign its Interior.Color value to a Long variable. A Long is suitable because Excel stores the color as a numeric value. You can then compare that value with an RGB color or use a conditional test to decide what the macro should do.

Sub CheckCellFillColor()
    Dim target As Range
    Dim fillColor As Long

    ' Inspect the direct fill of A1 on Sheet1.
    Set target = ThisWorkbook.Worksheets("Sheet1").Range("A1")
    fillColor = target.Interior.Color

    ' Report whether the fill matches the chosen blue.
    If fillColor = RGB(0, 112, 192) Then
        MsgBox "A1 has the specified blue fill."
    Else
        MsgBox "A1 has a different direct fill color."
    End If
End Sub

The comparison is exact: a slightly different shade is a different value. If a workbook uses several near-identical colors, decide whether exact matching is appropriate or whether the macro should compare a different property, such as a status value in another column. Color is often a poor substitute for data: a cell’s fill can be changed manually, copied accidentally, or controlled by a rule. When possible, make decisions from the underlying cell value and use color only to communicate the result.

A cell with no visible fill may return a color value that is not a useful way to distinguish “no fill” from a white fill. If the distinction matters, check the fill pattern as well as the color. In particular, Excel represents no fill with the pattern setting xlNone; a white fill is still a fill. This matters when a macro must preserve or detect formatting rather than just compare displayed shades.

Color Versus ColorIndex and Theme Colors

Interior.Color and Interior.ColorIndex are related but not interchangeable. Color is the actual color value and is usually the better option when you need a specific RGB shade or want to compare exact colors. ColorIndex refers to a numbered entry in Excel’s color palette. It can be convenient in older code or when a workbook deliberately relies on palette entries, but the number alone is less descriptive than an RGB value.

For example, assigning Interior.ColorIndex = 6 uses a palette entry, while assigning Interior.Color = RGB(255, 255, 0) explicitly requests yellow. A palette-based value may be less portable when a workbook’s palette differs from the one expected by the code. Avoid assuming an index will always mean a particular shade in every workbook; use RGB when the specific color matters.

Excel also supports theme colors. A fill may be linked to a theme color and a tint or shade, so it can change appearance when the workbook theme changes. Reading Interior.Color gives the resulting color value, while ThemeColor and TintAndShade describe theme-based formatting. Use theme properties when your goal is to preserve a workbook’s theme relationship; use a fixed RGB color when consistency of the actual shade is more important than theme adaptation.

Read the Visible Color from Conditional Formatting

One common surprise is that Interior.Color may not match the color a person sees. Conditional formatting can display a fill based on a cell’s value, a formula, or another rule without replacing the cell’s underlying direct fill. For a macro that needs the displayed result, use DisplayFormat.Interior.Color.

Sub ReadDisplayedFillColor()
    Dim target As Range
    Dim visibleColor As Long

    ' Read the displayed fill of A1, including conditional formatting.
    Set target = ThisWorkbook.Worksheets("Sheet1").Range("A1")
    visibleColor = target.DisplayFormat.Interior.Color

    MsgBox "Displayed fill value: " & CStr(visibleColor)
End Sub

DisplayFormat is intended for VBA procedures and can reflect conditional formatting as Excel displays it. It is not supported as a way to create a normal worksheet user-defined function; using it from a worksheet UDF can produce an error. Also, a displayed color is only a visual result at the time the macro reads it. If the cell value or conditional-formatting rule changes, the displayed fill may change too.

If a macro must identify why a cell is colored, reading the displayed color may not be enough. Multiple conditional-formatting rules can produce the same color, and rule priority can affect which format appears. In that case, inspect the cell’s value and the conditional-formatting rules themselves rather than relying solely on a color comparison.

Clear a Fill or Copy Formatting

To remove a cell’s fill without deleting its contents, set its interior pattern to xlNone. This removes the fill pattern; it does not clear the cell’s value or, by itself, remove font and border formatting. For example:

Sub ClearCellFill()
    Dim target As Range

    ' Select the cell whose fill should be removed.
    Set target = ThisWorkbook.Worksheets("Sheet1").Range("B2")

    ' Remove the fill while leaving the cell value in place.
    target.Interior.Pattern = xlNone
End Sub

To copy a fill from one cell to another, assign the source color to the destination. This copies the color value, not every aspect of the source formatting:

Sub CopyFillColor()
    Dim sourceCell As Range
    Dim destinationCell As Range

    ' Identify the source and destination cells.
    Set sourceCell = ThisWorkbook.Worksheets("Sheet1").Range("B2")
    Set destinationCell = ThisWorkbook.Worksheets("Sheet1").Range("C2")

    ' Copy the direct fill color only.
    destinationCell.Interior.Color = sourceCell.Interior.Color
End Sub

If you need to copy the complete formatting, including number formats, font, borders, and fill, use Excel’s copy-and-paste formatting methods instead of copying only Interior.Color. Be aware that copying a cell’s direct fill may not reproduce the visible color when conditional formatting on the destination applies a different result.

Common Problems and How to Diagnose Them

  • The macro colors the wrong sheet: An unqualified Range("A1") refers to the active sheet. Qualify the range through a worksheet, as in ThisWorkbook.Worksheets("Sheet1").Range("A1"), and confirm that the worksheet name exists.
  • The macro changes a different color than expected: Check the RGB components and verify that you are changing Interior.Color, not Font.Color. Also consider whether a theme or conditional-formatting rule affects the visible result.
  • The color comparison fails: Exact comparisons do not treat similar shades as equal. Confirm that the cell has the direct fill you expect, or read DisplayFormat.Interior.Color if a conditional-formatting rule supplies the visible fill.
  • The cell looks blank but reports a fill: A white fill can look like no fill against a white worksheet background. Check Interior.Pattern when the difference between an explicit fill and no fill matters.
  • The macro does not run: Confirm that the code is in a standard module, the workbook is saved in a macro-enabled format, and Excel’s macro security settings permit the code to run. Do not enable macros in an unfamiliar file just to inspect its formatting.

When debugging, inspect one known cell first, then test a small range before applying the code to a whole worksheet. A temporary message box or the VBA Immediate window can help confirm the range and value being read. Remove diagnostic output once the macro behaves as intended. This small check is especially useful in workbooks with hidden sheets, similarly named tabs, or conditional formatting.

Choosing the Right Method for the Task

For a fixed fill, use Interior.Color with RGB components. For a direct-color comparison, read that same property. For a cell whose visible fill comes from conditional formatting, read DisplayFormat.Interior.Color in a VBA procedure. Use ColorIndex mainly when working with code that intentionally uses a workbook palette, and use theme properties when preserving theme behavior is important.

For formatting based on values that change over time, Excel’s built-in conditional formatting may be a better alternative to a macro. On Windows, create a rule from Home > Conditional Formatting; the same ribbon feature is available in Excel for Mac, though ribbon layout can vary. A rule updates its displayed format as data changes, whereas a macro generally changes formatting only when it runs. Excel for the web can display many workbooks containing VBA, but it cannot run VBA macros; use desktop Excel for the VBA procedures described here.

Frequently Asked Questions

How do I change a cell color in Excel VBA?
Assign an RGB value to the cell’s Interior.Color property, such as Range("A1").Interior.Color = RGB(255, 0, 0). Qualify the range with its worksheet in a reusable macro to avoid changing the active sheet by mistake.

How do I get a cell’s color in VBA?
Read Range("A1").Interior.Color to get its direct fill color. If conditional formatting controls the visible fill, read Range("A1").DisplayFormat.Interior.Color from a VBA procedure instead.

How do I remove a cell fill with VBA?
Set Range("A1").Interior.Pattern = xlNone. This removes the fill without clearing the cell’s contents. It does not necessarily remove other formatting, such as a font color or border.

Why does VBA read a different color from the one I see?
The visible color may come from conditional formatting or a theme, while Interior.Color reports the cell’s direct fill. Try DisplayFormat.Interior.Color for the displayed result, and check whether a conditional-formatting rule is active.

Can Excel for the web run VBA cell-color macros?
No. Excel for the web can open and edit many workbooks that contain macros, but VBA macros run in desktop Excel. Use Excel for Microsoft 365 on Windows or the Mac desktop app to execute 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.