In Excel VBA, a cell’s fill color is read or set with Range("A1").Interior.Color, which uses a numeric color value rather than a text string such as #FF0000. To set a hex color, split its red, green, and blue components and pass them to VBA’s RGB function; for example, RGB(255, 0, 0) sets red.
What Interior.Color Represents
Interior is the object that represents a cell’s fill formatting, and its Color property gets or sets that fill as a numeric value. In ordinary use, you do not need to calculate the number yourself: give the property the result of RGB(red, green, blue), where each component is an integer from 0 through 255.
For example, RGB(255, 0, 0) means full red with no green or blue. Assigning it to Range("A1").Interior.Color fills A1 red. The property is not a hex-string property: assigning the text "#FF0000" directly is not the way to set a fill and can cause a type mismatch.
The number returned by Interior.Color can be useful when comparing colors or storing them in a variable. Declare such a variable as Long, the VBA integer type used for these color values. A cell can also have no fill or other pattern settings, so the color number alone does not describe every aspect of its appearance.
RGB Values and Hex Color Notation
Web and design tools commonly express a color as six hexadecimal digits in red-green-blue order: #RRGGBB. Each pair represents one channel, from 00 to FF, equivalent to decimal 0 through 255. Thus #336699 means red 51, green 102, and blue 153. The leading hash mark is a notation convention, not part of the numeric color.
VBA’s RGB function accepts those same channel values in red, green, blue order. Therefore, the direct translation of #336699 is RGB(51, 102, 153). A common source of confusion is that the underlying Windows color number is packed with the blue byte in the high-order position and red in the low-order position. Consequently, the hexadecimal digits shown by VBA’s Hex function may look reversed relative to the familiar #RRGGBB notation.
For instance, red is conventionally written #FF0000, but its VBA numeric value from RGB(255, 0, 0) is 255, whose hexadecimal representation is FF. That does not mean the color is blue. The safest practice is to use RGB for setting colors and explicitly rearrange the channels when translating a numeric value back to a six-digit hex string.
Set a Cell Fill with RGB
For a fixed color, assign RGB directly to the cell’s Interior.Color property. This example colors A1 a medium blue:
Sub SetCellFillExample()
' Set A1 to RGB(51, 102, 153), or #336699.
Range("A1").Interior.Color = RGB(51, 102, 153)
End Sub
To color a range, use the range address instead of a single cell reference. The following example applies a pale yellow fill to A2 through A10:
Sub SetRangeFillExample()
' Apply a pale yellow fill to the specified cells.
Range("A2:A10").Interior.Color = RGB(255, 242, 204)
End Sub
These examples use an unqualified Range, so Excel applies them to the active worksheet. In a larger macro, it is clearer and safer to qualify the worksheet explicitly, for example ThisWorkbook.Worksheets("Sheet1").Range("A1").Interior.Color. That prevents the result from depending on which sheet happens to be active.
Convert a Hex String to a VBA Color
If a color arrives from a website, brand guide, or configuration cell as a value such as #336699, convert its three channel pairs and pass them to RGB. The function below accepts either six digits or six digits preceded by a hash mark. It rejects other characters and incorrect lengths instead of silently treating an invalid value as a color.
Function ColorFromHex(ByVal hexText As String) As Long
Dim redValue As Long
Dim greenValue As Long
Dim blueValue As Long
Dim position As Long
Dim digit As String
' Allow an optional leading hash mark.
If Left$(hexText, 1) = "#" Then hexText = Mid$(hexText, 2)
If Len(hexText) <> 6 Then
Err.Raise vbObjectError + 1000, "ColorFromHex", _
"Enter six hexadecimal digits, optionally preceded by #."
End If
' Check that every character is a hexadecimal digit.
For position = 1 To 6
digit = UCase$(Mid$(hexText, position, 1))
If InStr(1, "0123456789ABCDEF", digit, vbBinaryCompare) = 0 Then
Err.Raise vbObjectError + 1001, "ColorFromHex", _
"The color contains a non-hexadecimal character."
End If
Next position
' Convert each pair from hex to decimal.
redValue = CLng("&H" & Mid$(hexText, 1, 2))
greenValue = CLng("&H" & Mid$(hexText, 3, 2))
blueValue = CLng("&H" & Mid$(hexText, 5, 2))
ColorFromHex = RGB(redValue, greenValue, blueValue)
End Function
For example, ColorFromHex("#336699") returns the same VBA color value as RGB(51, 102, 153). To use the function in a macro, first place it in a standard VBA module, then assign its result to a cell’s Interior.Color. The function returns a number, not a string suitable for display as a hex label.
Convert Interior.Color Back to Hex
To display a cell’s current fill using the familiar #RRGGBB format, extract the three bytes from the numeric value in red-green-blue order and format each as two digits. Do not simply put Hex(cell.Interior.Color) after a hash mark, because the packed numeric representation’s byte order is different.
Function ColorToHex(ByVal colorValue As Long) As String
Dim redValue As Long
Dim greenValue As Long
Dim blueValue As Long
' Unpack the color value into its RGB channels.
redValue = colorValue Mod 256
greenValue = (colorValue \ 256) Mod 256
blueValue = (colorValue \ 65536) Mod 256
ColorToHex = "#" & Right$("0" & Hex$(redValue), 2) _
& Right$("0" & Hex$(greenValue), 2) _
& Right$("0" & Hex$(blueValue), 2)
End Function
For example, if A1 is filled using RGB(51, 102, 153), passing Range("A1").Interior.Color to ColorToHex returns #336699. This is useful for exporting color settings or checking a workbook against a palette. The result describes the numeric fill color; it does not identify whether that color originally came from a theme, a direct RGB assignment, or another formatting source.
Interior.Color Compared with ColorIndex
Interior.Color represents a color value, while Interior.ColorIndex refers to an indexed color or a special setting such as automatic color or no color. Color indexes are a legacy approach and are less intuitive when matching a specific modern RGB or hex value. The same index should not be treated as a portable substitute for a documented RGB color.
For new code that needs a particular shade, use Interior.Color = RGB(...) or a hex-to-RGB conversion function. Use ColorIndex when you specifically need to work with indexed or special color settings. Also distinguish Interior.Color from Font.Color: the former changes a cell’s fill, while the latter changes the text color.
Theme Colors and Conditional Formatting
Excel’s theme colors can be linked to a workbook theme and may include a tint or shade. A direct assignment to Interior.Color gives the cell a specific color value; it does not preserve a link to a theme color in the same way as theme-based formatting. If a workbook needs coordinated colors that adapt when its theme changes, use Excel’s theme-color formatting features rather than treating a fixed hex code as theme-aware.
Conditional formatting is another important distinction. A cell may look colored because a conditional formatting rule is applying a fill, even though its ordinary cell fill is different. Changing Interior.Color does not necessarily change the rule or the color currently displayed by that rule. To change the conditional appearance, inspect the worksheet’s conditional formatting rules and update the applicable rule rather than assuming the direct fill property controls what you see.
For a quick check in Excel for Microsoft 365 on Windows, select the cell and use Home > Conditional Formatting > Manage Rules to review rules affecting the selection or worksheet. A visible fill that does not match the value your macro reads is often a formatting-source issue, not a failure of the RGB conversion.
Run the VBA Code Safely
VBA runs in the desktop versions of Excel, not in Excel for the web. On Windows, open the VBA editor from Developer > Visual Basic or press Alt+F11, then choose Insert > Module and paste the function or macro into the module. If the Developer tab is hidden, enable it through File > Options > Customize Ribbon.
Excel for Mac also supports VBA in the desktop app. The ribbon’s Developer > Visual Basic command opens the editor; keyboard shortcuts can differ by keyboard and system settings. Excel for the web can display a macro-enabled workbook, but it cannot run or edit its VBA macros. Save a workbook containing VBA as an Excel Macro-Enabled Workbook (.xlsm) so the code can be retained.
Before running a macro that changes formatting across many cells, save a separate copy of the workbook. VBA actions generally cannot be reversed with Ctrl+Z, so a mistaken range or color assignment may require closing without saving or restoring the backup. Only enable macros in workbooks from sources you trust.
Common Color Problems
- The color looks reversed in Hex output: The stored number is packed in a different byte order from
#RRGGBB. Extract red, green, and blue separately as shown above. - A hex value is rejected: Provide exactly six hexadecimal digits, with an optional leading
#. Three-digit shorthand such as#369is not accepted by the sample conversion function. - The cell still looks unchanged: Check for conditional formatting, a different active worksheet, or a range address that does not point to the intended cells.
- The font changed instead of the fill: Confirm that the code uses
Interior.Color, notFont.Color. - A fill or pattern is unexpected: The interior has properties beyond its color, including its pattern. Setting the color alone does not necessarily reset every existing fill-formatting property.
When diagnosing a macro, test it on one known cell first and inspect the exact worksheet and address. Then expand to a larger range only after the small test produces the expected visual result.
Frequently Asked Questions
Can I assign a hex string directly to Interior.Color?
No. Interior.Color expects a numeric color value. Convert the red, green, and blue pairs from a six-digit hex string and pass them to RGB, or use a conversion function such as ColorFromHex.
What is the VBA code for red?
Use Range("A1").Interior.Color = RGB(255, 0, 0) to fill A1 red. The equivalent conventional hex notation is #FF0000.
How do I read a cell’s fill color?
Read Range("A1").Interior.Color. It returns a numeric color value. Pass that value to a converter such as ColorToHex if you need a string in #RRGGBB format.
Why does Hex(Interior.Color) not match the cell’s hex code?
VBA’s packed color number does not store its bytes in the same order as the common red-green-blue hex notation. Reorder the red, green, and blue channels before formatting them as six hex digits.
Does Interior.Color show a conditional-formatting fill?
It represents the cell’s ordinary interior color, not necessarily the fill currently displayed by a conditional formatting rule. Inspect and edit the applicable rule when the visible color is rule-driven.
Related
- Excel Fill Colour Shortcut: Apply a Cell Colour
- Excel Shortcut to Capitalize All Letters in a Cell?
- Excel Sheet Keyboard Shortcuts: Essential Keys and Tips
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Calendar Date Picker: Options and SetupExcel for Microsoft 365 on Windows does not include a universal VBA calendar date-picker control that you can…
- Excel VBA UserForm Data Entry: Build a Simple FormTo enter data through an Excel VBA UserForm, create a form with text boxes and a Save button,…
- VBA Array Duplicate Values: Find, Count, and RemoveYes. VBA arrays can contain duplicate values; VBA does not automatically prevent or remove them. To find repeats,…
- Excel VBA Input Box Dropdown List: Options and SetupExcel’s built-in VBA InputBox does not support a dropdown list. To let someone choose from a visible list,…
- Excel VBA Text Color: Change Font and Cell Colors EasilyTo change text color in Excel VBA, set a range’s Font.Color property, usually with RGB(red, green, blue). For…
- Excel VBA Dim As Workbook: Declare and Use ItIn Excel VBA, declare a workbook variable with Dim wb As Workbook, then assign it to an open…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.