To set a cell’s fill color with VBA, assign a color to its Interior.Color property—for example, use RGB(255, 0, 0) for red. In Excel for Microsoft 365 on Windows, you can put that statement in a macro and run it on a cell or range; use Font.Color instead when you mean the text color.
What VBA changes when you set a color
Excel stores a cell’s fill separately from its font formatting. The Interior object controls the background fill, while the Font object controls text appearance. This distinction matters: changing a cell’s fill does not change its text color, and changing the font color does not recolor the background.
The most direct fill assignment is Range("A1").Interior.Color. A range can be a single cell, a rectangular block, or a reference built from a worksheet variable. Assigning one color to a multi-cell range applies that fill to every cell in the range. For example, setting Range("B2:B8").Interior.Color colors all seven cells alike.
Use a fully qualified worksheet reference when the macro should always change a particular sheet. An unqualified reference such as Range("A1") refers to the active sheet, which can be a different sheet from the one you intended if the user changes tabs before running the macro.
Set a cell or range color with RGB
RGB(red, green, blue) is usually the clearest way to specify an exact color in VBA. Each of its three arguments is an integer from 0 through 255: the first controls red, the second green, and the third blue. Combining the three values produces a wide range of colors. For instance, RGB(255, 0, 0) is red, RGB(0, 0, 0) is black, and RGB(255, 255, 255) is white.
To try a macro in Excel for Microsoft 365 on Windows, press Alt+F11 to open the Visual Basic Editor, choose Insert > Module, paste the code into the module, and run the procedure. The sample colors B2 yellow, its text dark red, and B3:B8 light blue. It also removes the fill from B9.
Sub SetCellColors()
' Change these cells on the active worksheet.
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Range("B2").Interior.Color = RGB(255, 235, 156)
ws.Range("B2").Font.Color = RGB(156, 0, 6)
ws.Range("B3:B8").Interior.Color = RGB(221, 235, 247)
ws.Range("B9").Interior.Pattern = xlNone
End Sub
Before running any macro, save a copy of the workbook. VBA changes cannot be reversed with Ctrl+Z, so a backup is the practical way to recover if a macro affects the wrong cells. If you want to keep the macro in the workbook, save it as an Excel Macro-Enabled Workbook (.xlsm); the standard .xlsx format does not retain VBA code.
Choose between RGB, ColorIndex, and theme colors
RGB is a good default when you need a particular color to remain consistent regardless of the workbook’s theme. The values are explicit and easy to reuse, and they work for both fill and font colors. A shared RGB value is also convenient when applying the same formatting to many ranges in one procedure.
ColorIndex uses an index into Excel’s color palette rather than three color components. It can be useful when working with older workbooks or code that already uses palette indexes, but it is less descriptive than RGB. An index does not tell a reader the color at a glance, and palette-based colors may not correspond to the intended shade in every workbook context. If exact appearance matters, RGB is generally easier to understand and maintain.
Theme colors are linked to the workbook’s theme. That relationship is useful when a workbook should follow a coordinated design: changing the theme can update theme-linked formatting. The trade-off is that a theme color’s displayed appearance depends on the workbook theme and, in some cases, a tint or shade. Use a theme-linked color when coordinated design matters; use RGB when a fixed color is the priority.
- Choose RGB for a specific, repeatable shade or when the macro should be easy to read.
- Choose ColorIndex mainly when maintaining existing code that already relies on palette indexes.
- Choose theme colors when the fill should adapt to the workbook’s design theme.
Color many cells based on a value
A common next step is to color cells according to their contents. A loop lets a macro inspect each cell and choose a fill. This example assumes status labels are in A2:A20 on the active worksheet. It colors cells containing the exact text “Late” red and colors other cells in the range green. Change the range, text, or RGB values to match your workbook.
Sub ColorStatuses()
' Color each status cell according to its value.
Dim ws As Worksheet
Dim cell As Range
Set ws = ActiveSheet
For Each cell In ws.Range("A2:A20")
If cell.Value = "Late" Then
cell.Interior.Color = RGB(255, 199, 206)
Else
cell.Interior.Color = RGB(198, 239, 206)
End If
Next cell
End Sub
This simple version treats every value other than “Late”—including a blank—as green. If blanks should remain uncolored, add a separate check for an empty cell before the Late test. Also consider whether the rule should match case, partial text, or a specific data type; a comparison against a complete text value is not the same as searching for a word within a longer note.
For ongoing, automatically updated formatting, Excel’s Conditional Formatting feature may be a better fit than a macro. Conditional formatting responds to cell values as they change, while a macro normally applies the fill only when you run it. Use VBA when you need a repeatable action, want to format a selected range on demand, or are automating other workbook tasks alongside the color change.
Remove a fill or restore a color
To remove a fill, set the cell’s interior pattern to xlNone, as the B9 line in the earlier macro does. This removes the fill rather than painting the cell white. That distinction is useful because a white fill is still a fill and can cover a background color or visual effect that would otherwise show through.
To change a cell back to a particular color, assign that color explicitly. VBA does not automatically know which fill was present before your macro changed it. If the original formatting must be restored later, preserve the relevant values or design the macro to apply a known standard rather than assuming that a cleared fill is the same as the original appearance.
Common problems and how to prevent them
- The wrong sheet changes: An unqualified range refers to the active sheet. Set a worksheet variable and use
ws.Rangewhen the target sheet is known. - The color looks different than expected: Check whether the workbook uses a theme color, whether a conditional formatting rule is affecting the display, and whether the RGB components are in the intended red-green-blue order.
- The macro will not run: Confirm that the workbook is saved in a macro-enabled format and that macros are permitted under your organization’s security settings. Do not enable macros in files from untrusted sources.
- Nothing changes in Excel for the web: Excel for the web can open and edit many macro-enabled workbooks, but it cannot create, run, or edit VBA macros. Use the desktop Excel application for VBA.
- Ctrl+Z does not undo the macro: Excel’s normal undo stack does not undo VBA actions. Save a separate copy before running a macro that changes formatting across many cells.
If a fill appears to ignore the macro’s color, inspect Conditional Formatting rules for that range. A conditional format can control the displayed fill when its rule applies, making the result look as though a direct fill assignment had no effect. Adjust or remove the rule if the direct fill is supposed to be visible, or use conditional formatting itself for value-driven colors.
Windows, Mac, and macro availability
The VBA properties shown here, including Interior.Color, are used in desktop Excel on both Windows and Mac. The menus and keyboard shortcuts for opening the Visual Basic Editor can differ by platform and keyboard configuration. On Windows, Alt+F11 opens the editor and Alt+F8 opens the Macro dialog. On a Mac, Option+F11 and Option+F8 may be the corresponding shortcuts; some keyboards require holding Fn as well, and shortcut behavior can vary with system settings. You can also open the editor through Excel’s available macro or developer tools.
VBA macros do not run in Excel for the web. If the workbook will be used by people who only have browser access, consider conditional formatting or another web-compatible approach instead. On any desktop platform, macro security is intentional: run code only when you trust its source, and check your organization’s policies before changing security settings.
Frequently Asked Questions
How do I set a cell’s background color in VBA?
Assign a value to the cell’s Interior.Color property, such as RGB(255, 0, 0) for red. Use a worksheet-qualified range if the macro should target a specific sheet.
How do I change the text color instead of the fill?
Use the cell’s Font.Color property. For example, assign an RGB value to Range("A1").Font.Color to change the text without changing the background.
What does RGB mean in Excel VBA?
RGB stands for red, green, and blue. VBA’s RGB function accepts three values from 0 to 255 and combines them into a color value that can be assigned to a cell’s fill or font.
How can I clear a cell’s fill color with VBA?
Set the cell’s Interior.Pattern property to xlNone. This removes the fill instead of applying a solid white fill.
Can a VBA macro color cells automatically when their values change?
A macro can apply colors when it runs, but it does not automatically rerun just because a value changes. Use Conditional Formatting for color rules that should update as values change, or use event-driven VBA if a macro-based response is required.
Can I undo a VBA color change with Ctrl+Z?
No. Excel’s standard undo command does not reverse macro actions. Save a copy before running code that changes cell formatting, especially when it affects a large range.
Related
- Excel Shortcut for Inserting Rows Above or Below
- Excel Shortcut to Go to a Specific Column in Seconds
- Excel Shortcut to Insert a Cut Row
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA UserForm Progress Bar: Build One That WorksTo add an Excel VBA UserForm progress bar, create a UserForm with a Frame and a colored Label…
- VBA Sort in Two Columns: Code, Steps, and Key OptionsYes. In Excel VBA, sort the complete data range and pass two keys to its Sort method: the…
- Excel Macros Disabled: How to Enable Them SafelyTo enable a macro in Excel for Microsoft 365 on Windows, open a workbook you trust and select…
- Text Macro Examples for Excel: Practical VBA TasksText macro examples for Excel include cleaning extra spaces, changing text to uppercase, adding a prefix, and replacing…
- Excel VBA: Set a Cell’s Background Color with RGB ValuesTo set a cell’s background color in Excel VBA, assign a color to its Interior.Color property—for example, Range("B2").Interior.Color…
- VBA Create New Workbook With Name: A Practical GuideIn Excel VBA, create a workbook with Workbooks.Add, then give it a lasting name and location with SaveAs.…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.