Excel VBA Cell Interior Color: Read, Set, and Clear

VBA & macros · Updated November 19, 2026

In Excel VBA, a cell’s fill color is controlled through its Interior object: set it with Range("A1").Interior.Color = RGB(255, 230, 153), read it with Range("A1").Interior.Color, and remove the fill with Range("A1").Interior.Pattern = xlNone. The examples below explain what these properties return, how to apply them to ranges, and why a displayed color may not match the value VBA reads.

What Interior.Color Represents

Every Excel cell has an Interior object that exposes its fill settings. The Color property represents the fill color as a VBA Long value. In practice, the simplest way to assign a specific color is to use VBA’s built-in RGB function, which takes red, green, and blue components in that order. Each component must be a whole number from 0 through 255.

For example, RGB(255, 0, 0) specifies red, RGB(0, 0, 0) specifies black, and RGB(255, 255, 255) specifies white. The color value returned by Interior.Color is numeric, so it may not look like a familiar RGB triplet when displayed in the Immediate window or a message box. To compare colors, compare the property to another RGB result rather than trying to interpret the number by eye.

Interior also includes properties such as Pattern, ColorIndex, ThemeColor, and TintAndShade. These describe different aspects of the fill. Color is usually the most direct option when a macro needs a fixed color; the other properties are useful for patterns, palette-based workbooks, or theme-aware formatting.

Set a Cell Fill Color with VBA

To run a macro in Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, choose Insert > Module, and paste a procedure into the module. Change the worksheet name and address to match the workbook. Before running a macro that changes formatting, save a copy of the workbook: macro actions cannot be undone with Ctrl+Z.

Sub SetCellFillColor()
    ' Set a fixed RGB fill on one cell.
    Dim target As Range
    Set target = ThisWorkbook.Worksheets("Sheet1").Range("A1")
    target.Interior.Color = RGB(255, 230, 153)
End Sub

This assigns a pale orange fill to A1 on Sheet1. ThisWorkbook means the workbook containing the code; if the macro is stored in another workbook, use an explicitly named workbook or adjust the reference. A worksheet name that does not exist causes a run-time error, so check spelling, spaces, and punctuation.

For a particular shade, change the three RGB components. For example, RGB(0, 112, 192) assigns a blue fill. A useful way to obtain the components is to use Excel’s fill-color picker and inspect a custom color’s RGB values, then put those three values into the VBA call. The RGB function creates the numeric color value; it does not accept a hex color string such as #0070C0.

Read and Compare a Cell’s Color

Reading a fill uses the same property as setting it, but without an assignment. Store the returned value in a Long variable. This example reports whether A1 has the same fill as the pale orange color used above:

Sub CheckCellFillColor()
    ' Compare the stored fill color with a known RGB value.
    Dim target As Range
    Dim fillColor As Long
    Set target = ThisWorkbook.Worksheets("Sheet1").Range("A1")
    fillColor = target.Interior.Color
    If fillColor = RGB(255, 230, 153) Then
        MsgBox "A1 has the expected fill color."
    Else
        MsgBox "A1 has a different fill color."
    End If
End Sub

Exact comparison is appropriate when the macro itself set the fill or when the workbook uses a known color. It can be less helpful in a workbook where users apply theme colors, tints, or conditional formatting. A color that looks similar can have a different stored value, and a fill that appears on screen because of a rule may not be represented by the cell’s ordinary Interior.Color property.

Remove a Fill Without Clearing Other Formatting

To remove a cell’s fill while keeping its font, borders, number format, and other formatting, set its pattern to xlNone:

Sub ClearCellFill()
    ' Remove the fill from A1 without clearing other cell formatting.
    Dim target As Range
    Set target = ThisWorkbook.Worksheets("Sheet1").Range("A1")
    target.Interior.Pattern = xlNone
End Sub

This is different from assigning white. A white fill is still a fill, whereas xlNone removes the fill pattern and allows the cell’s background to show through. It is also different from using Excel’s Clear Formats command, which removes a broader set of formatting. Choose the narrow operation when the goal is only to remove a cell’s background color.

Setting Interior.ColorIndex to xlColorIndexNone is another way to express no fill in code that uses the color-index property. For new code, Interior.Pattern = xlNone makes the intention clear: remove the fill pattern. If the apparent fill remains afterward, check whether conditional formatting is coloring the cell.

Apply a Color to a Range or Several Cells

The Interior property works with a multi-cell range as well as one cell. Assigning one color to a rectangular range applies that fill to every cell in the range. For instance, replace Range("A1") with Range("A1:C10") in the set-color example to fill all 30 cells. A non-contiguous range can also be addressed, but a simple rectangular range is easier to inspect and maintain.

When formatting a range that depends on worksheet data, avoid selecting cells with Select or activating the sheet unless the macro needs to interact with the user interface. Refer directly to the worksheet and range, as in the examples. This reduces reliance on whichever sheet happens to be active when the macro runs. For example, a range reference such as ThisWorkbook.Worksheets("Summary").Range("B2:B20") is explicit and less likely to target the wrong sheet.

Remember that a range assignment applies the same color to all of its cells; it does not create a gradient or choose colors based on values. To apply different colors according to a rule, use a loop in VBA or, for many worksheet-based highlighting tasks, Excel’s Conditional Formatting feature.

Choose Between Color, ColorIndex, and Theme Colors

Use Interior.Color with RGB when you want a specific, fixed color. This method is readable, works well for ordinary VBA formatting, and does not depend on a workbook’s indexed palette. It is a good default for a macro that marks errors, highlights a heading, or applies a consistent status color.

Interior.ColorIndex refers to a color index rather than directly to an RGB value. It can be relevant in older workbooks or code designed around an indexed palette, but an index is less descriptive than an RGB triplet and may not communicate the intended shade clearly. Avoid replacing a known RGB color with a guessed index; the two properties are not interchangeable numeric scales.

Theme colors are useful when a workbook should follow its document theme. A theme color can change appearance if someone changes the workbook’s theme, while a fixed RGB color remains tied to its specified color value. ThemeColor and TintAndShade let VBA work with a theme choice and its tint or shade. Choose them when matching the workbook’s design is more important than preserving one exact RGB value. For a one-off, precise color, Color = RGB(...) is generally simpler.

Conditional Formatting and the Color VBA Sees

A frequent source of confusion is conditional formatting. A cell can look colored because a conditional-formatting rule applies a fill, even though the cell’s direct fill setting is different or absent. Range("A1").Interior.Color reads the cell’s ordinary interior formatting; it does not necessarily report the color currently displayed after conditional formatting is evaluated.

For a macro that needs to inspect the displayed formatting, Excel provides DisplayFormat. For example, Range("A1").DisplayFormat.Interior.Color can read the effective displayed fill, including conditional formatting. This property is intended for reading the displayed result; use the rule itself or the cell’s Interior property to change formatting. Conditional formatting can also depend on the current value, other cells, or rule priority, so the displayed result can change after recalculation or an edit.

For ordinary rule-based highlighting, the ribbon feature is often easier to maintain than a macro that repeatedly paints cells. In Excel for Microsoft 365 on Windows, select the cells and choose Home > Conditional Formatting. On Mac, the Conditional Formatting command is also on the Home tab, though the ribbon layout can differ. Excel for the web can create and edit many conditional-formatting rules, but it does not run VBA macros; use desktop Excel to run the code.

Common Problems and How to Fix Them

  • The macro runs but changes the wrong sheet. A bare Range("A1") refers to the active sheet. Qualify it with the intended worksheet, such as ThisWorkbook.Worksheets("Sheet1").Range("A1").
  • The cell looks colored but the comparison fails. Check whether conditional formatting supplies the visible fill. If so, read DisplayFormat.Interior.Color when you need the displayed color, or inspect the rule that creates it.
  • The color is close but not an exact match. Confirm the RGB components and compare the stored value to RGB(red, green, blue). Theme tints and similar-looking shades can have different underlying values.
  • Removing the fill appears to do nothing. A conditional-formatting rule may still color the cell. Also check whether you assigned white instead of removing the pattern; white is a visible fill, not no fill.
  • The procedure cannot find the worksheet. Verify the sheet tab name exactly. A renamed sheet, an extra space, or a misspelled name can prevent the worksheet reference from resolving.

On Mac, VBA is available in desktop Excel, but keyboard shortcuts and editor access can vary with the keyboard and system settings. Use Tools > Macro > Visual Basic Editor if a Windows Alt+F11 shortcut is unavailable. Excel for the web does not execute VBA, so open the workbook in the desktop app to run or edit a macro.

Frequently Asked Questions

How do I set a cell background color in Excel VBA?
Assign an RGB value to the cell’s interior: Range("A1").Interior.Color = RGB(255, 0, 0) sets A1 to red. Qualify the range with the worksheet name when the macro should not depend on the active sheet.

How do I get the RGB color of a cell in VBA?
Read Range("A1").Interior.Color into a Long variable. To test for a known color, compare it with an RGB(red, green, blue) value; the returned number is not displayed as a three-part RGB label.

How do I remove a cell’s fill color in VBA?
Use Range("A1").Interior.Pattern = xlNone. This removes the fill pattern without clearing unrelated formatting. If a color remains visible, check the cell’s conditional-formatting rules.

Why does Interior.Color not match the color I see?
The displayed fill may come from conditional formatting rather than the cell’s direct interior setting. Read DisplayFormat.Interior.Color to inspect the effective displayed fill, or inspect the conditional-formatting rule.

Can Excel for the web run an Excel VBA macro?
No. Excel for the web can work with many workbook features, but it does not run VBA. Open the workbook in desktop Excel for Windows or Mac to execute VBA 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.