In Excel VBA, vbRed, vbBlue, and the other vb color constants are built-in names for eight basic colors. Assign them to a cell’s Interior.Color or Font.Color property; for any other shade, use the VBA RGB function.
What VB color constants mean
The vb color constants come from VBA, not from an Excel-specific color list. They are convenient named values for common colors, so a line such as Range("A1").Interior.Color = vbYellow is easier to recognize than a numeric color value. These constants can also be used with other VBA properties that accept a color value.
VBA provides eight standard constants:
vbBlack— blackvbRed— redvbGreen— bright greenvbYellow— yellowvbBlue— bluevbMagenta— magentavbCyan— cyanvbWhite— white
They are constants, not procedures: type the name directly, without quotation marks or parentheses. VBA is not case-sensitive, so vbRed and VBRED refer to the same constant, although the usual mixed-case spelling is easier to read.
How the constants map to RGB values
Each constant represents a familiar RGB color. In the table, the RGB triplet gives the red, green, and blue channel levels, each on a scale from 0 to 255. The decimal value is the number VBA returns for that color in Excel’s Color property.
| Constant | RGB value | Decimal value |
|---|---|---|
vbBlack |
RGB(0, 0, 0) | 0 |
vbRed |
RGB(255, 0, 0) | 255 |
vbGreen |
RGB(0, 255, 0) | 65280 |
vbYellow |
RGB(255, 255, 0) | 65535 |
vbBlue |
RGB(0, 0, 255) | 16711680 |
vbMagenta |
RGB(255, 0, 255) | 16711935 |
vbCyan |
RGB(0, 255, 255) | 16776960 |
vbWhite |
RGB(255, 255, 255) | 16777215 |
A frequent point of confusion is vbGreen: it means full-intensity green, RGB(0, 255, 0), which looks much brighter than many greens used in spreadsheets. If you want a darker shade, choose explicit RGB values instead of assuming the constant means a particular Excel theme color.
Apply a VB color to a cell
Excel exposes separate properties for different parts of a cell’s appearance. Interior.Color controls the fill, while Font.Color controls the text. A border has its own Color property. For example, to fill A1 yellow and make its text red, the assignments are:
Range("A1").Interior.Color = vbYellow
Range("A1").Font.Color = vbRed
These lines can be used inside a procedure in a standard VBA module. To open the VBA editor on Windows, press Alt+F11, or use Developer > Visual Basic. On a Mac, use Developer > Visual Basic; the Option+F11 shortcut may require the Fn key depending on the keyboard. Excel for the web does not run VBA macros, though it can open some macro-enabled workbooks.
For a more reusable example, this procedure colors one cell with a custom blue fill and white text. It assumes the workbook contains a worksheet named Sheet1; change the sheet name or cell reference if needed.
Sub ColorExample()
Dim ws As Worksheet
Dim target As Range
' Select the worksheet and cell to format.
Set ws = ThisWorkbook.Worksheets("Sheet1")
Set target = ws.Range("B2")
' Apply a custom blue fill and white font.
target.Interior.Color = RGB(30, 144, 255)
target.Font.Color = vbWhite
End Sub
Before running a macro, save a copy of the workbook. VBA changes cannot be undone with Ctrl+Z, and the equivalent Mac undo shortcut, Command+Z, does not reverse macro changes either. Run a procedure from the editor or use Developer > Macros; Alt+F8 opens the macro list on Windows, while Option+F8 is commonly used on Mac keyboards.
Use RGB for colors beyond the basic eight
The RGB function creates a color from red, green, and blue components. Each argument must be an integer from 0 through 255: zero contributes none of that channel, and 255 contributes its maximum. For example, RGB(30, 144, 255) makes a vivid blue. RGB(80, 80, 80) makes a neutral dark gray because all three channels have the same value.
Use RGB when you need to match a brand color, reproduce a color from a design specification, or choose a less intense fill than vbGreen. It is usually clearer to write the channel values directly than to copy an unfamiliar decimal number. If a color comes from a web or design tool as a hexadecimal code such as #1E90FF, convert its pairs to decimal channel values: 1E is 30, 90 is 144, and FF is 255, so the VBA expression is RGB(30, 144, 255).
RGB is useful for fixed colors, but it does not automatically follow a workbook’s theme. A precisely specified RGB fill will generally stay that color when someone changes the theme, while a theme-based color can adapt to the workbook’s palette. Consider which behavior is desirable before setting colors in a template that others will customize.
Color versus ColorIndex
Excel VBA also has the ColorIndex property. It refers to an indexed color rather than directly specifying an RGB triplet. In older Excel formats, the workbook’s palette could be customized, so a particular index was not necessarily a universal color. This makes code using values such as ColorIndex = 3 less self-explanatory and potentially dependent on the workbook’s palette.
For most new VBA code, prefer Color with a vb constant or RGB when the goal is to specify the actual color. Use ColorIndex when you specifically need to work with indexed colors or maintain a workbook built around that system. Excel also defines special values such as xlColorIndexAutomatic and xlColorIndexNone for relevant formatting properties; these are not ordinary RGB colors.
Do not confuse a color’s numeric value with its position in a palette. The number 255, for example, is the value of vbRed when used as a color, but it is not the same thing as palette index 255. The two properties represent different ways of identifying color.
Theme colors and conditional formatting
Modern Excel workbooks offer theme colors, which connect formatting to a workbook theme and its accent palette. VBA can work with theme-related formatting through properties such as ThemeColor and TintAndShade on supported formatting objects. Those properties are useful when a template should adapt if its theme changes. By contrast, the Color property and RGB values specify a direct color.
There is another important distinction: a macro can set a cell’s fill, but conditional formatting can apply a fill dynamically when a rule is true. If a color is meant to reflect changing values, a conditional formatting rule may be a better fit than a macro that colors cells once. Find the rules in Excel for Windows at Home > Conditional Formatting > Manage Rules. On Mac, use Home > Conditional Formatting to create or manage rules; ribbon wording and layout can vary slightly by release.
When a cell’s displayed color does not match the fill set by a macro, check whether conditional formatting applies to that cell. A conditional format can affect what appears on screen without changing the cell’s underlying direct fill in the way a simple inspection of Interior.Color might suggest. Also check whether the macro targets the intended worksheet and range.
Common mistakes and troubleshooting
Most color-related VBA problems come from assigning a valid value to the wrong property, using an invalid RGB channel, or overlooking another formatting rule. Check these points when the result is unexpected:
- For a cell background, use
Interior.Color; for text, useFont.Color. - Pass three channel values between 0 and 255 to
RGB. A value outside that range is not a valid channel specification. - Use a worksheet and range that actually exist. A misspelled sheet name in a reference such as
Worksheets("Sheet1")causes a runtime error. - Check conditional formatting if the displayed color seems to override the direct fill.
- Remember that a macro may change many cells at once and cannot be reversed through the normal Undo command.
For a multi-cell range, assigning a color to the range applies the formatting to its cells. For example, Range("A2:A10").Interior.Color = vbYellow fills the entire specified range. If only some cells should be colored based on their values, use a loop with a condition or, often more simply, conditional formatting.
Choosing the right color method
Choose the method that matches how the workbook will be used, rather than selecting a color property only because it is familiar. For a quick, readable macro that uses one of the basic colors, a vb constant is concise. For a precise fixed shade, use RGB. For formatting that should follow a workbook’s theme, use theme-related formatting. For colors that respond to data, consider conditional formatting.
Accessibility and readability matter too. A bright fill does not guarantee that text on top of it will be easy to read; check the contrast between font and background, especially when a workbook will be printed or viewed on different screens. Avoid relying on color alone to communicate meaning. Pair a colored status with a word, symbol, or other clear indicator so that readers who cannot distinguish the colors still get the information.
Frequently Asked Questions
What are the VBA color constants?
The eight standard constants are vbBlack, vbRed, vbGreen, vbYellow, vbBlue, vbMagenta, vbCyan, and vbWhite.
How do I change a cell’s background color with VBA?
Set its Interior.Color property, for example Range("A1").Interior.Color = vbYellow. Use Interior.Color = RGB(30, 144, 255) for a custom fill.
How do I change font color in VBA?
Set the range’s Font.Color property, such as Range("A1").Font.Color = vbRed. This changes the text color, not the cell fill.
Can I use HTML hex colors in VBA?
Not directly as a CSS-style string. Convert the hex code’s red, green, and blue pairs to decimal values, then pass those three values to RGB.
Why does vbGreen look bright?vbGreen is RGB(0, 255, 0), the maximum green channel with no red or blue. Use a lower green channel or add red and blue with RGB to create a softer shade.
Related
- Excel Shortcut to Select All Rows in a Column at Once
- Excel Data Validation from a List: Setup and Troubleshooting
- Excel Named Ranges: How to Create and Use Them
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Loop Array: Examples, Bounds, and Best PracticesA VBA array loop visits array elements one at a time, usually with a For loop using LBound…
- Excel VBA Copy an Entire Column: Safe Code and TipsYes. In Excel for Microsoft 365 on Windows, VBA can copy a complete column with a worksheet’s Columns…
- VBA Macro Error Handling: Patterns, Examples, and FixesVBA macro error handling lets a procedure respond to runtime problems instead of stopping without explanation. Use On…
- VBA Open an Excel File and Copy Data into a WorkbookTo open an Excel file and copy its data with VBA, use Workbooks.Open to open the source workbook,…
- Excel VBA Date and Time Formatting: Practical ExamplesTo format a date and time in an Excel cell with VBA, set the cell or range’s NumberFormat…
- VBA Resize Pictures to Fit in Excel Cells AutomaticallyTo resize pictures to fit inside Excel cells with VBA, run a macro that finds each picture, scales…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.