In Excel VBA, set a cell’s font color with the Font.Color property, usually by assigning an RGB value—for example, Range("A1").Font.Color = RGB(255, 0, 0) makes the text red. Use Font.ColorIndex for Excel’s indexed colors, and remember that conditional formatting can change the color that appears on screen even when VBA has set a different base font color.
What Font.Color Controls
Excel’s object model separates a cell’s font formatting from its interior, or fill, formatting. The Font object controls the appearance of characters: color, bold, italic, size, name, and other text attributes. The Interior object controls the cell background. That means Range("A1").Font.Color changes the text, while Range("A1").Interior.Color changes the fill.
This distinction is a frequent source of confusion: a cell can have red text on a yellow background, black text on no fill, or any other combination. Setting the font color does not change the fill, and setting the fill does not change the font. Both properties belong to a Range object, so the same approach works on a single cell, a contiguous range, or a range selected by a worksheet reference.
The value stored by Font.Color is a numeric color value, not the word “red” or a web-style hexadecimal string. VBA’s RGB function creates a value from red, green, and blue components, each from 0 through 255. For example, RGB(0, 0, 0) is black and RGB(255, 255, 255) is white.
Set Font Color with RGB
RGB is generally the clearest way to specify an exact color because the three components make the intended shade explicit. For instance, RGB(0, 102, 204) creates a medium blue. In a macro, qualify the worksheet and range so the operation does not depend on which sheet happens to be active.
Before running a macro, save a copy of the workbook: VBA changes cannot be reversed with Ctrl+Z. This example colors the text in B2:B10 blue. Change the worksheet name and range to match your workbook.
Sub SetFontColorRGB()
Dim ws As Worksheet
Dim target As Range
' Set the worksheet and cells to format.
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set target = ws.Range("B2:B10")
' Apply a blue font color.
target.Font.Color = RGB(0, 102, 204)
End Sub
The macro changes only the font color. It does not alter values, formulas, number formats, or cell fills. If the target is a multi-cell range, Excel applies the same font color to every cell in that range. For a non-contiguous range, a Range reference can include multiple areas, but explicit references are easier to review and maintain than relying on the current selection.
Choose Between Color, ColorIndex, and ThemeColor
Excel exposes several ways to choose font color, and they serve different purposes:
Font.Colorsets or reads a numeric color value. AssigningRGB(red, green, blue)is convenient when a precise shade matters.Font.ColorIndexuses an index into Excel’s color palette. It is useful for older workbooks or code that already relies on indexed colors, but the index is less descriptive than RGB.Font.ThemeColorrefers to a color in the workbook’s theme. A theme-linked color can follow theme changes, which is useful when a workbook’s design may be recolored consistently.
For a new macro that needs a fixed shade, RGB is usually the most readable choice. A theme color is more appropriate when the workbook deliberately uses theme colors and should adapt if its theme changes. ColorIndex may be necessary for compatibility with older code, but do not assume an index will communicate the same intended shade as a named color or a specific RGB triplet.
Theme-related formatting can also include tint and shade. The TintAndShade property adjusts a color’s lightness or darkness, but theme properties and direct color assignments are not interchangeable in intent: choosing an explicit RGB value prioritizes a fixed shade, while using the theme supports a design that can respond to theme changes.
Apply Colors to Cells and Ranges
You can write directly to a cell’s Font property or store the range in a variable first. The direct form is concise for a one-off change, while a named worksheet and range make larger procedures easier to understand. Avoid unqualified references such as Range("A1") in reusable code: they refer to the active sheet, which may not be the sheet your macro was meant to format.
To format a row, column, or block, use an ordinary range address such as ws.Rows(1).Font.Color, ws.Columns("C").Font.Color, or ws.Range("A1:D20").Font.Color. Formatting an entire column can affect many more cells than expected, so use a bounded range when practical. If the cells have mixed existing font formatting and the goal is to recolor only certain entries, loop through individual cells or identify the qualifying cells before applying the change.
A macro can also apply a color based on a condition, such as coloring negative values red. In that case, use a loop that checks each cell’s value and sets that cell’s Font.Color only when the condition is met. Ensure the code accounts for blanks, text, and formula errors if those can occur in the range; comparing every cell as if it were a clean number can produce unexpected results.
Read or Reset a Font Color
To inspect a cell’s color in VBA, read its Font.Color property into a numeric variable. The returned number is useful for comparison with a known RGB value, though it is not especially readable in a watch window. Reading a single cell is straightforward; reading a multi-cell range with inconsistent font colors is less useful because there is no single color that describes every cell. Check cells individually when mixed formatting is possible.
To return font coloring to Excel’s automatic setting, assign xlColorIndexAutomatic to Font.ColorIndex. Automatic is not the same as a guaranteed black font: Excel chooses a suitable default based on the workbook and display context. Font color also does not have the same “no color” meaning commonly used for a cell fill; use automatic font color when you want Excel’s default text color.
To remove direct formatting more broadly, Excel’s Home > Clear > Clear Formats command clears formatting from selected cells, not just font color. Use it carefully because it also removes other formatting, such as fills and number formats. A macro that changes only Font.Color is safer when preserving the rest of the cell’s appearance matters.
Why the Visible Color May Not Change
If a macro assigns a font color but the worksheet still displays another color, check for conditional formatting first. Conditional formatting can apply a font color when its rule is true, and that displayed result may take precedence over the ordinary formatting set through Font.Color. The base color may have changed even though the cell still looks the same under the active rule.
Review the relevant cells under Home > Conditional Formatting > Manage Rules. Check which rules apply, their conditions, and their order. If the desired color should respond to cell values, it may be better to change the conditional-formatting rule rather than repeatedly setting a static font color with VBA. If the color should be permanent formatting, adjust or remove the conflicting rule where appropriate.
Other common causes include changing the wrong worksheet, using an incorrect range address, or running code that refers to the active workbook instead of the workbook containing the target data. Qualifying references with ThisWorkbook.Worksheets("Sheet1") helps avoid these mistakes. Also confirm that the macro actually ran and that the workbook was saved after the change.
Run VBA in Excel and Save the Workbook
VBA requires the desktop version of Excel; Excel for the web does not run VBA macros. In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, insert a standard module if needed, paste the procedure, and run it from the editor. On Excel for Mac, Option+F11 opens the editor on many keyboards; depending on function-key settings, you may need Fn+Option+F11. You can also open the Visual Basic Editor from Excel’s Developer tab. The editor and some macro-related details differ between Windows and Mac.
A workbook containing VBA should be saved in a macro-enabled format such as Excel Macro-Enabled Workbook (*.xlsm). Saving as a regular .xlsx file does not preserve the VBA project. Excel’s macro security settings may also block macros; enable macros only for files and code you trust. Since macro actions cannot be undone with Ctrl+Z, keep a backup before running code that changes many cells.
Good Practices for Reliable Formatting Macros
Font-color macros are simple, but a few habits help prevent accidental changes and make the code easier to reuse:
- Declare variables and use explicit worksheet references rather than relying on the active sheet or selection.
- Use RGB when a precise, fixed color is important; use theme colors when the workbook should follow its theme.
- Keep the target range as narrow as possible, especially when the macro runs repeatedly or handles a large worksheet.
- Separate the rule that decides which cells qualify from the instruction that applies formatting, making the logic easier to inspect.
- Test on a copy and check the result in Excel, including any conditional-formatting rules that might affect the display.
These practices matter particularly in shared workbooks. A hard-coded RGB value gives consistent output, but may not match a team’s design standards. A theme color may fit the workbook better but can change when the theme changes. Choosing deliberately is more reliable than selecting whichever property happens to be shortest to type.
Frequently Asked Questions
How do I change font color in Excel VBA?
Assign a color to the range’s Font.Color property, for example, Range("A1").Font.Color = RGB(255, 0, 0). In a macro, qualify the range with its worksheet to ensure the code targets the intended cell.
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 are separate properties, so changing one does not change the other.
How do I set a font color to automatic?
Set Font.ColorIndex to xlColorIndexAutomatic. Excel then chooses the default font color; this does not promise that the result will always be black.
Why does conditional formatting override my VBA font color?
A conditional-formatting rule can supply the color that Excel displays when its condition is true. Review the applicable rules through Home > Conditional Formatting > Manage Rules, and edit the rule if the displayed color should be controlled by the condition.
Can Excel for the web run a VBA font-color macro?
No. Excel for the web does not run VBA macros. Use the desktop Excel app to run VBA, and save the workbook in a macro-enabled format such as .xlsm to retain its code.
Related
- Excel: Copy Subtotal Rows Only Without Detail Rows
- How to Hyperlink to Another Cell in Excel
- How to Group Rows in Excel and Hide Them
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel Macro Keyboard Shortcuts: Assign and Run MacrosIn Excel for Microsoft 365 on Windows, assign a macro shortcut by choosing Developer > Macros, selecting the…
- Excel VBA Bold Text: Cells, Ranges, and Partial TextIn Excel VBA, make a cell or range bold by setting its Font.Bold property to True, such as…
- VBA Loop Continue: How to Skip to the Next IterationVBA has no built-in Continue For, Continue Do, or general Continue statement. To skip the rest of the…
- VBA Loop Without Do: Choosing For, While, or For EachYes. In VBA, you can loop without using Do: use For...Next for a known number of repetitions, For…
- VBA Loop Example: For, Do While, and For Each LoopsA VBA loop repeats a block of code, either a set number of times or until a condition…
- Read a Text File with Excel VBA: Methods and ExamplesYes. In Excel for Microsoft 365 on Windows, VBA can read a plain-text file with the Open and…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.