Excel Cell Color by Hex: Apply, Match, and Display

Formulas & functions · Updated November 19, 2026

Yes. In Excel for Microsoft 365, you can apply a cell fill that corresponds to a hex color, but a normal worksheet formula cannot turn a hex value into a fill color by itself. Use the color picker for a fixed color, conditional formatting for a known set of hex codes, or VBA when a cell’s fill needs to follow an arbitrary hex code entered in another cell.

What “color by hex” means in Excel

A hex color is a text representation of a color’s red, green, and blue components. The common format is six characters, written as #RRGGBB: the first pair gives red, the second green, and the third blue. For example, #1F4E79 represents red 31, green 78, and blue 121, a dark blue often used in tables and headings.

Excel can display a cell with that color, but a cell’s fill is formatting, not its value. A formula can compare text such as #1F4E79, but formulas do not directly set a fill color. This distinction matters when a workbook contains a column of hex codes: the codes can be stored as text, while a separate formatting method is needed to paint cells to match.

Hex codes also usually describe opaque colors. A six-character code does not specify transparency, and a cell fill should not be assumed to support an alpha channel simply because another graphics format uses eight-character hex codes.

Apply a hex color manually

For one or a few cells, choose a custom fill from the ribbon. Select the cell, then go to Home > Fill Color (the paint-bucket arrow) > More Colors. In the color dialog, enter the color’s red, green, and blue values if the dialog provides RGB fields. Some Microsoft 365 color-picker interfaces also provide a hex field; availability and labels can vary by Excel build and platform.

If the dialog only accepts RGB, convert each pair of the hex code to its decimal equivalent. For #1F4E79, enter red 31, green 78, and blue 121. Remove the leading # before reading the pairs. The selected cell then receives a fixed fill; changing the text in a nearby cell will not change that fill automatically.

To reuse the same manually chosen color, copy the formatted cell and use Home > Paste > Paste Special > Formats, or use Format Painter. These approaches copy formatting rather than creating a live link to a hex value, so they are best for static layouts, report headings, and one-off highlights.

Use conditional formatting for known hex codes

Conditional formatting is useful when the hex text indicates one of a small, known number of categories. For instance, suppose A2:A100 contains hex-code text and you want the matching cells in B2:B100 to turn red whenever the code is #FF0000. Select B2:B100, then choose Home > Conditional Formatting > New Rule and select the option to use a formula to determine which cells to format.

With hex codes in A2:A100 and the corresponding cells to color in B2:B100, use this rule formula:

=$A2="#FF0000"

Set the rule’s fill using Format, choose a red fill, and confirm. The dollar sign fixes the reference to column A, while the row number adjusts as Excel evaluates each row. This rule tests whether the text matches a specific code; it does not read the code’s RGB components and create a matching fill automatically. For other known codes, add separate rules and choose a fill for each one.

Conditional formatting is a good fit for status labels such as green, yellow, and red, provided the set of colors is limited and each has a corresponding rule. It can become cumbersome if users can enter hundreds of different hex codes, because each distinct color would need its own formatting rule and selected fill.

Make the fill follow an arbitrary hex value with VBA

If a cell should take on any valid color typed as hex, a short VBA macro can read the selected cell’s value, convert its three pairs to RGB, and set its fill. The example below expects the active cell to contain either six hex characters, such as 1F4E79, or the same code with a leading #. It colors that cell itself.

In desktop Excel for Windows, open the Visual Basic Editor from Developer > Visual Basic, insert a standard module, and paste the code. If the Developer tab is hidden, enable it through File > Options > Customize Ribbon. Return to the sheet, select a cell containing the code, and run ApplyHexFillToActiveCell from Developer > Macros.

Sub ApplyHexFillToActiveCell()
    Dim hexValue As String
    Dim redPart As Long
    Dim greenPart As Long
    Dim bluePart As Long

    ' Read a code such as #1F4E79 or 1F4E79 from the active cell.
    If IsError(ActiveCell.Value2) Then
        MsgBox "The active cell must contain a hex color code.", vbExclamation
        Exit Sub
    End If

    hexValue = Trim$(CStr(ActiveCell.Value2))
    If Len(hexValue) = 7 And Left$(hexValue, 1) = "#" Then
        hexValue = Mid$(hexValue, 2)
    End If

    If Len(hexValue) <> 6 Or Not (hexValue Like "[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]") Then
        MsgBox "Enter six hexadecimal characters, optionally preceded by #.", vbExclamation
        Exit Sub
    End If

    ' Convert each two-character pair and apply the fill.
    redPart = CLng("&H" & Mid$(hexValue, 1, 2))
    greenPart = CLng("&H" & Mid$(hexValue, 3, 2))
    bluePart = CLng("&H" & Mid$(hexValue, 5, 2))
    ActiveCell.Interior.Color = RGB(redPart, greenPart, bluePart)
End Sub

Save a macro-enabled workbook with the .xlsm extension if you need to keep the macro. Save a separate copy before running or adapting macros: VBA changes cannot be undone with Ctrl+Z. Excel for the web cannot run VBA, though desktop Excel can open a workbook that contains macros. Excel for Mac has a Visual Basic Editor too, but menu placement and keyboard shortcuts can differ; the macro’s RGB conversion is VBA rather than a worksheet function.

Choose the method that fits the workbook

  • One fixed color: Use the fill color picker and set a custom color. This is quick and leaves the workbook free of code.
  • A few fixed categories: Use conditional formatting rules. The color changes when the tested cell value changes, but each category needs a rule and a chosen fill.
  • Many user-entered hex colors: Use VBA in desktop Excel, or consider an Office Script if the workbook environment supports scripts. These options can convert codes programmatically rather than requiring a separate conditional-formatting rule for each color.
  • A shared workbook where macros are not allowed: Prefer manual formatting or a manageable set of conditional-formatting rules. Check organizational policy before using scripts or macros.

Office Scripts are a separate automation option in supported Microsoft 365 environments. A script can set a fill using a color string such as #1F4E79, but scripts are not worksheet formulas and are not available in every account or organization. If you need a color to update automatically whenever a cell is edited, check that your chosen automation can be triggered in the way your workflow requires; a manually run script will not necessarily update on every edit.

Common problems and checks

If a color looks wrong, first check the order of the pairs. Hex is red, then green, then blue; swapping the pairs changes the color. Confirm that the code has six valid hexadecimal characters, 0–9 or A–F, and remove spaces. A leading # is common in web color notation, but some Excel input fields expect only the six characters.

When conditional formatting appears not to work, check the rule’s Applies to range and the relative row reference in its formula. For a rule applied to B2:B100 that reads A2:A100, =$A2="#FF0000" is usually the intended pattern. If the code has an unnoticed space or uses a different spelling, the exact comparison will not match. Also check rule order and whether another rule’s formatting takes precedence.

A manually applied fill can remain after the hex text changes because it is not linked to that text. Similarly, changing the color of a cell that has conditional formatting may not appear to work: the conditional rule can control the displayed fill while it applies. Review or remove the relevant rule through Home > Conditional Formatting > Manage Rules.

Finally, remember that a color is not a reliable substitute for data. Keep a text label, status, or hex code alongside the visual fill when readers need to sort, filter, interpret, or print the workbook. Color appearance can also differ across monitors, themes, and print settings, so important distinctions should not depend on color alone.

Frequently Asked Questions

Can an Excel formula change a cell’s fill color from a hex code?
No. A normal worksheet formula can compare or manipulate text, but it cannot set cell formatting. Use conditional formatting for known values, or VBA or supported Office Scripts for programmatic fills.

Can I type a hex code directly into Excel’s color picker?
Some Microsoft 365 color-picker interfaces expose a hex input, while others provide RGB fields instead. If there is no hex box, convert the code to its red, green, and blue decimal values and enter those in the custom color dialog.

Will conditional formatting turn any hex code into its exact color?
Not by itself. A conditional-formatting rule can test a code and apply a fill you select for that rule. It does not dynamically convert every possible code into its corresponding color.

Does the VBA macro work in Excel for the web?
No. Excel for the web does not run VBA macros. Use desktop Excel for the macro, or investigate Office Scripts if they are available and suitable for your Microsoft 365 account.

Why does my cell keep its old color after I change the code?
A manually selected fill is static, so editing nearby text does not update it. A macro must be run again, or an appropriate conditional-formatting rule must control the displayed fill.

Related

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.