To set a cell’s text color in Excel VBA, change the cell’s Font.Color property, usually with the RGB function: Range("A1").Font.Color = RGB(255, 0, 0) makes the text in A1 red. Use a worksheet-qualified range when the macro should target a particular sheet, and save a copy before running it because VBA changes cannot be undone with Ctrl+Z.
The basic VBA method
Excel stores text formatting in a cell’s Font object. Its Color property sets the font color; it does not change the cell’s fill. The shortest form is Range("A1").Font.Color = RGB(255, 0, 0). In a macro, the range is usually qualified with the worksheet name so the code does not accidentally format a cell on whichever sheet happens to be active.
The RGB arguments represent red, green, and blue intensity, each from 0 through 255. For example, 255, 0, 0 is red; 0, 0, 255 is blue; and 0, 0, 0 is black. White is 255, 255, 255. You can set a range of cells in the same way: every cell in that range receives the specified font color.
For a one-off operation, you can also select cells and run a macro that uses Selection.Font.Color. That approach is convenient, but it depends on the current selection and active sheet. A direct reference such as ThisWorkbook.Worksheets("Sheet1").Range("A1").Font.Color is more predictable when the macro is part of a repeatable process.
Choosing an RGB color
The RGB function is a readable way to specify a fixed color. It returns a numeric color value that VBA can assign to Font.Color. Using named values or constants for frequently reused colors can make a larger project easier to maintain. For example, a workbook might use the same red for overdue items and the same dark green for approved items.
Here are useful examples, each of which can replace the color assignment in a macro:
RGB(255, 0, 0)sets pure red.RGB(0, 128, 0)sets a medium green.RGB(0, 0, 255)sets pure blue.RGB(255, 255, 255)sets white, which may be difficult to read on a white fill.
RGB values are not the only way to describe a color, but they are useful when the color must remain the same regardless of a workbook’s theme. Choose a color with enough contrast against the cell’s fill, especially if the sheet will be printed or read by people with color-vision differences. Color should not be the only signal of meaning: labels, symbols, or clear status text can make the information more accessible.
Targeting one cell, a range, or a worksheet
Always decide explicitly what the macro should format. A single cell reference changes only that cell. A range such as A2:A20 changes each cell in that range, and a whole-column reference can affect every cell in a worksheet column. Large ranges may take longer to process and can make formatting less intentional, so limit the target to the data that needs it.
For example, this assignment sets the font color for every cell in A2:A20 on the named worksheet:
Worksheets("Sheet1").Range("A2:A20").Font.Color = RGB(192, 0, 0)
The unqualified Worksheets collection refers to the active workbook. Use ThisWorkbook.Worksheets when the code should refer to the workbook that contains the macro, rather than another workbook that a user may have activated. The worksheet name in the reference must match the tab name exactly. If a sheet may be renamed, a worksheet code name or a deliberate lookup strategy can make the project more resilient.
To format only cells that meet a condition, VBA can test their values and set the font color selectively. For a small, one-time range, a loop is easy to follow. For a rule that should update automatically whenever values change, Excel’s Conditional Formatting feature may be a better fit; the distinction is covered below.
A complete macro example
This example colors the text in A2:A20 on Sheet1. Change the worksheet name, range, or RGB numbers to suit the workbook. It uses explicit object variables so the target is clear rather than relying on the active selection.
Sub SetTextColorInRange()
Dim ws As Worksheet
Dim target As Range
' Refer to the sheet in the workbook that contains this macro.
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set target = ws.Range("A2:A20")
' Set the font color to a dark red.
target.Font.Color = RGB(192, 0, 0)
End Sub
In Excel for Microsoft 365 on Windows, open the VBA editor with Alt+F11, then choose Insert > Module in the editor and paste the procedure into the module. If the Developer tab is not visible, enable it through File > Options > Customize Ribbon and select Developer. On Mac, VBA is available in the desktop Excel app; open the editor with Option+F11 (some keyboards also require Fn), or use Developer > Visual Basic if that tab is available. The ribbon customization controls are under Excel > Preferences > Ribbon & Toolbar on Mac.
Save a macro-enabled workbook as an Excel Macro-Enabled Workbook (.xlsm) if you need to keep the VBA code. A standard .xlsx file does not store VBA project code. Macros do not run in Excel for the web, so open the workbook in the desktop application to run this procedure. Before running a macro, save a separate copy of the workbook: formatting changes made by VBA cannot be reversed with Ctrl+Z on Windows or Command+Z on Mac.
Font color, fill color, and theme color
A frequent source of confusion is the difference between text color and cell background color. Font.Color changes the letters and numbers displayed in the cell. Interior.Color changes the cell’s fill. For example, setting a red font does not create a red background; to change the background, the relevant property is Range("A1").Interior.Color.
Another option is Font.ColorIndex, which uses Excel’s indexed colors and special values such as automatic color. It can be useful in older code that already uses the indexed palette, but RGB is generally clearer when you need to specify a particular fixed color. Theme colors are useful when a workbook’s design should follow its theme. A theme-based font color can change when the workbook theme changes, whereas an RGB assignment stays tied to its numeric color.
Choose based on the intended behavior, not just the shortest syntax:
- Use
Font.Color = RGB(...)for a specific, fixed color. - Use a theme color when the formatting should coordinate with the workbook theme.
- Use
Font.ColorIndexwhen maintaining code that uses Excel’s indexed color system. - Use
Interior.Colorwhen the goal is to color the cell background rather than its text.
VBA formatting or Conditional Formatting?
A VBA assignment writes formatting to the cells when the macro runs. It does not automatically reconsider the color when a value later changes. For instance, a macro that colors a status cell red because it currently says “Late” will leave that font red if someone later replaces the text with “Paid,” unless another macro changes it again.
Conditional Formatting is often preferable for rules that must respond automatically to cell values. In Excel for Microsoft 365 on Windows, select the range and use Home > Conditional Formatting to create a rule; the exact rule option depends on whether the condition is a text match, a comparison, or a formula. On Mac, the Conditional Formatting controls are also on the Home tab. Conditional formats can set font color and fill, and Excel recalculates their appearance as values change.
There is an important visual detail: a conditional format can override the font color set directly by VBA while its rule applies. If a macro appears to have run but the displayed text color does not change, inspect the cell’s conditional formatting rules before assuming the VBA assignment failed. Conversely, if you want a VBA macro to control the visible color, remove or adjust conflicting rules rather than repeatedly assigning a direct font color that the rule masks.
Common problems and practical checks
If the code runs without an obvious error but the wrong cell changes, check whether the reference is unqualified. Range("A1") by itself generally refers to the active sheet in the active workbook. A user clicking another tab before running the macro can therefore change the target. Qualifying the range with a worksheet reference avoids that particular source of surprises.
If Excel reports a “Subscript out of range” error on the worksheet line, check the tab name for spelling, spaces, and punctuation. A macro using ThisWorkbook also looks in the workbook that contains the code; if the target sheet is in a different workbook, that reference must be changed deliberately. Do not silently assume that the active workbook is always the intended one.
If color seems to disappear, check whether another macro, a conditional format, or a later formatting operation is changing the same cell. If a color is hard to see, compare it with the fill and test the sheet at normal zoom or in a print preview. Very light font colors can be effectively invisible on a white background. Finally, test the procedure on a small sample range first, especially if it targets an entire column or a production workbook.
For ordinary cell text, Font.Color applies to the cell’s font formatting. Excel also supports character-level formatting through the Characters collection for certain text in a cell, but that is different from coloring the entire cell and has extra limitations. If the request is simply to make a cell’s displayed text red or blue, use Font.Color; do not use Interior.Color or a character-level operation unnecessarily.
Frequently Asked Questions
How do I make a cell’s text red in VBA?
Assign a red RGB value to the cell’s font color: Range("A1").Font.Color = RGB(255, 0, 0). Qualify the range with the worksheet when the macro should always change a particular sheet.
What is the difference between Font.Color and Interior.Color?Font.Color changes the text color. Interior.Color changes the cell’s fill color. They control separate parts of a cell’s formatting.
Can VBA change the text color of a whole range?
Yes. Apply the property to a multi-cell range, such as Range("A2:A20").Font.Color = RGB(0, 0, 255). All cells in that range receive the same font color.
Why does my VBA color not appear?
Check that the macro targets the intended worksheet and that the color contrasts with the fill. A Conditional Formatting rule may also be overriding the direct font color while the rule applies.
Can I undo a VBA color change with Ctrl+Z?
No. Running a VBA macro clears Excel’s normal undo history, so Ctrl+Z cannot reverse the macro’s formatting changes. Save a copy before running code that changes a workbook.
Does VBA run in Excel for the web?
No. Excel for the web can open macro-enabled workbooks but does not run VBA macros. Use the desktop Excel application for Windows or Mac to run the procedure.
Related
- Excel Shortcut to Insert a Copied Row
- Excel Shortcut for Inserting Rows Above or Below
- Excel Shortcut to Go to a Specific Column in Seconds
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Long Strings: Limits and Practical FixesIn Excel VBA, use a variable-length String to hold long text; it can theoretically contain about 2 billion…
- Excel VBA: Change Cell and Range FormatsTo change a cell’s format with Excel VBA, assign a property such as NumberFormat, Font.Bold, or Interior.Color to…
- VBA Function Return Multiple Values: What WorksYes. A VBA function can provide multiple results, but it has only one formal return value. To deliver…
- Excel VBA: Check Whether a Cell Contains TextTo check whether a cell contains a particular string in Excel VBA, use InStr and test whether its…
- Excel VBA Button: Add, Assign, and Fix Macro ButtonsAn Excel VBA button is a clickable control that runs a macro in a workbook. In Excel for…
- Excel VBA: Loop Through Rows in a Range EfficientlyTo loop through every row in an Excel range with VBA, use For Each rowRange In dataRange.Rows when…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.