Excel VBA: How to Change a Cell’s Color

VBA & macros · Updated November 19, 2026

To change a cell’s color with Excel VBA, set its Interior.Color property, such as Range("B2").Interior.Color = RGB(255, 230, 153) for a pale yellow fill. Put the code in a VBA module, run the macro, and save the workbook as a macro-enabled file if you want to keep the code.

Change a cell color with a simple macro

In Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor. Choose Insert > Module, then paste this procedure into the module:

Sub ColorOneCell()
    ' Color cell B2 pale yellow.
    Range("B2").Interior.Color = RGB(255, 230, 153)
End Sub

Click inside the procedure and press F5, or return to Excel, choose Developer > Macros, select ColorOneCell, and choose Run. The fill of B2 changes; its value and formula, if any, remain untouched. If the Developer tab is missing, enable it in File > Options > Customize Ribbon by checking Developer. On Mac, use Developer > Visual Basic to open the editor; keyboard shortcuts can depend on the keyboard and macOS settings.

Before running any macro, save a copy of the workbook. Excel’s Undo command, including Ctrl+Z on Windows or Command+Z on Mac, cannot undo VBA actions. Keep a separate original if the workbook contains important data or formatting.

How Interior.Color and RGB work

A cell’s fill is its interior formatting. In VBA, Interior.Color sets that fill to a color value. The built-in RGB(red, green, blue) function makes a color from three numbers, each from 0 to 255. The first number controls red, the second green, and the third blue. For example, RGB(255, 0, 0) is red, RGB(0, 128, 0) is a dark green, and RGB(255, 255, 255) is white.

Use Interior.Color when you want a specific color rather than relying on a theme position. If you prefer a different fill, change the RGB values and run the macro again. You can also set the font color separately with Font.Color; changing the fill does not automatically choose a readable font color. For example, a dark fill may need white text to maintain contrast.

VBA also offers Interior.ColorIndex, which refers to an index in Excel’s color palette. It can be convenient for legacy code, but its palette-based meaning is less direct than an RGB value, especially when workbooks use different palettes or themes. For new code that requires a consistent exact color, RGB is generally clearer.

Color a range or a group of cells

Use a range reference instead of a single cell to apply the same fill to several cells. This macro colors B2 through B10:

Sub ColorRange()
    ' Apply one fill to a contiguous range.
    Range("B2:B10").Interior.Color = RGB(198, 239, 206)
End Sub

For nonadjacent cells, separate addresses with commas inside the range string. For example, Range("B2,B5,B8") refers to three individual cells. To color a whole row or column, use a reference such as Rows(2) or Columns("B"), but consider the effect: an entire worksheet row or column contains many cells, and applying formatting to all of them can increase file size or slow work on a large workbook.

If the code is stored in a standard module, an unqualified Range refers to the active worksheet. That can be risky if a user switches sheets before running the macro. Identify the workbook and worksheet explicitly when the target must not depend on what is active:

Sub ColorFixedSheetRange()
    ' Change the sheet name to match the workbook.
    ThisWorkbook.Worksheets("Report").Range("B2:B10").Interior.Color = RGB(198, 239, 206)
End Sub

ThisWorkbook means the workbook that contains the macro. The worksheet name in the example must match the actual tab name; otherwise, VBA reports an error. This explicit reference is useful in workbooks with multiple sheets or when macros are launched from another workbook.

Color cells based on their values

A macro can choose a fill based on a cell’s contents. For instance, this example colors a status cell green when it contains the word Complete and red otherwise. It assumes the macro is intended to evaluate B2:

Sub ColorStatus()
    Dim statusCell As Range

    Set statusCell = ThisWorkbook.Worksheets("Report").Range("B2")
    If statusCell.Value = "Complete" Then
        statusCell.Interior.Color = RGB(198, 239, 206)
    Else
        statusCell.Interior.Color = RGB(255, 199, 206)
    End If
End Sub

This is a one-time check: the color changes when the macro runs, not automatically every time the value changes. To update the fill when a user edits a cell, place a Worksheet_Change event procedure in the code module for the relevant sheet, rather than in a standard module. The example below watches A2:A100 and colors each edited cell in that range light yellow:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim watched As Range
    Dim changed As Range

    Set watched = Me.Range("A2:A100")
    Set changed = Application.Intersect(Target, watched)
    If changed Is Nothing Then Exit Sub

    ' Color the edited cell or cells.
    changed.Interior.Color = RGB(255, 230, 153)
End Sub

Open the target worksheet’s code module in the Visual Basic Editor and paste the event procedure there. It can handle a multi-cell paste within the watched range. A change event responds to edits, not to a formula result changing solely because another cell recalculated; for that situation, a different event or a formula-based formatting rule may be more appropriate.

Choose between VBA and conditional formatting

VBA is useful when color is part of a larger task, when a button or other macro should apply formatting, or when you need a repeatable operation across worksheets. It can also set formatting after a data import or other automated process. However, macros require a desktop version of Excel that supports VBA, and users may need to allow the workbook’s macros to run.

For colors that should update automatically as values change, Excel’s Conditional Formatting is often simpler and safer. Select the cells, then use Home > Conditional Formatting to create a rule. For example, Highlight Cells Rules can format values that meet a comparison, while a formula-based rule can handle more tailored conditions. Conditional formatting is dynamic: Excel evaluates its rules as data changes. A VBA fill, by contrast, is generally static until the macro runs again.

Conditional formatting can also affect what color appears on screen even when VBA has set an underlying fill. If a rule applies to a cell, the rule’s displayed format can take precedence over the ordinary cell formatting. If a macro seems to have no visible effect, inspect Home > Conditional Formatting > Manage Rules and check which rules apply to the selected cells. Do not assume the VBA assignment failed just because the visible color did not change.

Common errors and how to fix them

  • The macro changes the wrong sheet. An unqualified Range operates on the active sheet. Use ThisWorkbook.Worksheets("Report").Range("B2") when a specific sheet is required, and confirm the sheet name exactly.
  • The code does not run. Check that the workbook is saved as .xlsm if it needs to retain VBA, and that macros are permitted by your organization’s security settings. Do not enable all macros indiscriminately; only run code from a source you trust.
  • The cell stays the same color on screen. Check for conditional formatting rules, and confirm that the code targets the cell you are viewing. Also verify that the macro actually ran and did not stop on an error.
  • The RGB color looks different than expected. RGB uses red, green, blue order, with each value between 0 and 255. Recheck the order and values; RGB(0, 0, 255) is blue, not red.
  • The workbook loses the macro. Saving as a standard .xlsx file does not retain VBA code. Choose Excel Macro-Enabled Workbook (.xlsm) when saving a workbook that must keep its macros.

To troubleshoot, step through the procedure with F8 in the Visual Basic Editor and watch which line is reached. A misspelled worksheet name, a protected sheet, or a range reference that is not what you expect can stop or misdirect the operation. If the worksheet is protected, formatting changes may be blocked unless the sheet is unprotected or the protection was set up to permit formatting. Avoid changing protection settings without understanding their purpose.

Running macros safely in Excel

VBA macros do not run in Excel for the web. Open the workbook in the desktop Excel app to edit or run its VBA. On Windows, macro security settings are available through File > Options > Trust Center > Trust Center Settings > Macro Settings; on Mac, the Trust Center and macro preferences are managed through Excel’s application settings. The exact options can depend on the platform and organization policies. A company-managed device may block macros regardless of a workbook’s settings.

Save a working copy before testing, particularly if a procedure colors a large range or changes formatting across multiple sheets. A macro’s formatting changes cannot be reversed with the normal Undo command. If a procedure affects more cells than intended, close without saving and reopen the backup, or use a deliberately designed macro that restores the prior formatting. Do not run unfamiliar macros just because a workbook asks you to enable content; macros can do much more than change colors.

Frequently Asked Questions

What VBA code changes a cell’s fill color? Use Range("B2").Interior.Color = RGB(255, 230, 153) to give B2 a pale-yellow fill. Replace B2 with the cell you want and adjust the three RGB values for another color.

How do I change the font color instead of the fill? Use the cell’s Font.Color property, for example Range("B2").Font.Color = RGB(255, 0, 0). That sets the text color to red and leaves the cell’s fill unchanged.

Can Excel VBA change a cell color automatically? Yes. Put a Worksheet_Change procedure in the relevant worksheet’s code module to respond to edits. If the desired color depends on a formula result, consider conditional formatting, which can update as Excel recalculates.

Why does my VBA color not appear? The macro may be targeting a different worksheet, or a conditional formatting rule may be overriding the displayed fill. Confirm the range, run the macro, and inspect the rules applied to the cell.

Can I run a VBA color macro in Excel for the web? No. Excel for the web can open and edit many workbook contents, but it cannot run VBA macros. Use the desktop version of Excel to run 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.