To get a cell’s fill or font color as a HEX code in Excel for Microsoft 365 on Windows, add the VBA function below, then enter =ExcelColorCode(A2,"fill") in a worksheet cell, replacing A2 with the colored cell. For RGB values without VBA, open the color menu and choose More Colors. The VBA method is useful when you need a copyable HEX code such as #4472C4.
Excel stores cell colors, but its standard ribbon does not provide a simple command that copies an existing color as HEX. The steps below explain how to read a cell’s directly applied fill or font color, what the results mean, and what to do if the displayed color comes from a theme or conditional formatting instead.
What You’ll Need
- Excel for Microsoft 365 desktop on Windows and a workbook containing the colored cell. VBA does not run in Excel for the web.
- Permission to use macros, if you want the HEX-code function. Save a copy of the workbook before adding or running VBA.
- For an RGB-only check, no code is needed; Excel’s More Colors dialog can show RGB values for a selected color.
Step-by-Step
- Identify the color you need. Click the cell whose color you want to read. Decide whether you need the cell’s background (fill) or the text color (font). A cell can have different colors for each, so be clear about which one you are checking. If the cell appears colored because of conditional formatting, read the troubleshooting section first: the function below reads direct formatting, not the conditional-formatting display.
- Open the Visual Basic Editor. In desktop Excel for Windows, press
Alt+F11. Alternatively, choose Developer > Visual Basic if the Developer tab is visible. If it is not, enable it through File > Options > Customize Ribbon and select Developer. In Excel for Mac, use Tools > Macro > Visual Basic Editor, or the Visual Basic button on the Developer tab; the keyboard shortcut can depend on your Mac’s function-key settings. Excel for the web does not include the VBA editor. - Add a standard module. In the Visual Basic Editor, choose Insert > Module. A blank code pane appears. Make sure you insert a module, rather than pasting the function into a worksheet’s code page or the ThisWorkbook page. A standard module makes the function available to worksheet formulas.
- Paste this function into the module. It returns a HEX value beginning with # for a direct fill or font color. It returns “No fill” for a cell without a fill and “Automatic” when the font color is set to automatic.
Public Function ExcelColorCode(ByVal Target As Range, Optional ByVal ColorType As String = "fill") As Variant
Dim colorValue As Long
Dim redValue As Long
Dim greenValue As Long
Dim blueValue As Long
On Error GoTo InvalidInput
' Accept one cell and either its fill or font color.
If Target Is Nothing Then GoTo InvalidInput
If Target.Cells.CountLarge <> 1 Then GoTo InvalidInput
Select Case LCase$(Trim$(ColorType))
Case "fill"
If Target.Interior.Pattern = xlNone Then
ExcelColorCode = "No fill"
Exit Function
End If
colorValue = Target.Interior.Color
Case "font"
If Target.Font.ColorIndex = xlColorIndexAutomatic Then
ExcelColorCode = "Automatic"
Exit Function
End If
colorValue = Target.Font.Color
Case Else
GoTo InvalidInput
End Select
' Excel stores the color components in a Long value.
redValue = colorValue Mod 256
greenValue = (colorValue \ 256) Mod 256
blueValue = (colorValue \ 65536) Mod 256
ExcelColorCode = "#" & Right$("0" & Hex$(redValue), 2) _
& Right$("0" & Hex$(greenValue), 2) _
& Right$("0" & Hex$(blueValue), 2)
Exit Function
InvalidInput:
ExcelColorCode = CVErr(xlErrValue)
End Function
The backslashes in the VBA divisions are integer-division operators. The ampersands join the three two-digit hexadecimal components; together they form the familiar red-green-blue HEX order. This function handles a single cell and recognizes the words fill and font, ignoring capitalization and extra spaces.
- Return to Excel and save in a macro-enabled format. Close or switch away from the Visual Basic Editor. Choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm) if the workbook contains macros. If you save it as a regular .xlsx file, Excel removes the VBA code when saving. Only enable macros in workbooks you trust.
- Enter a formula for the fill color. With the colored cell in A2, enter this formula in a different cell. The layout assumed here is that A2 contains the cell whose direct background fill you want to inspect; the formula returns its HEX code.
=ExcelColorCode(A2,"fill")
For example, if A2 has a solid blue fill with RGB components 68, 114, and 196, the result is #4472C4. The first pair, 44, represents red; 72 represents green; and C4 represents blue, with each decimal component converted to hexadecimal. Do not type a leading apostrophe or omit the # if another system expects the standard six-digit HEX format.
- Read the font color instead, if needed. With the same layout—A2 is the single cell whose text color you want—use this formula in another worksheet cell. The second argument tells the function to read the font rather than the fill.
=ExcelColorCode(A2,"font")
To inspect another cell, change A2 to its address. To reuse the result elsewhere, copy the returned HEX value as text or use the result cell in your other work. If the formula displays #VALUE!, check that the function is in a standard module, the reference points to one cell, and the second argument is exactly fill or font.
Get RGB values without VBA: Select a cell, open Home > Fill Color ▼ > More Colors for its fill, or Home > Font Color ▼ > More Colors for its text. In the color dialog, open the Custom tab and read the Red, Green, and Blue values. Depending on your Excel build, the dialog may also offer a HEX field. This method is convenient for a one-off check, but choosing or editing a color in the dialog can change the cell if you confirm a different color. Cancel if you only wanted to inspect it. Menu labels and dialog layout can vary slightly on Mac.
Common Mistakes
- Checking the wrong part of the cell. Fill color is the background; font color is the text. Use the matching second argument in the formula. A blue background does not mean the text is blue.
- Expecting a theme name instead of a HEX value. A theme color can be described by its theme position and tint, but the function returns the resulting RGB color as HEX. If you change the workbook theme later, the displayed color may change even though the old HEX result remains the same.
- Using a multi-cell range. The function is designed for one cell at a time. A reference such as A2:A10 is not a single color request and returns an error. Use one formula per cell if you need several results.
- Saving as .xlsx after adding VBA. The .xlsx format does not retain VBA code. Save as .xlsm and keep a backup. If Excel warns that features cannot be saved in a macro-free workbook, do not choose the option that discards the code.
- Assuming formatting changes update the formula immediately. Excel does not normally recalculate a worksheet formula just because you changed a cell’s formatting. The old result may remain until calculation is forced.
Tips
- Use uppercase HEX consistently when sharing values. HEX letters are not case-sensitive, but uppercase values such as
#4472C4are easy to scan. - Keep a small reference table with the source cell, a label, and the returned code. That makes it easier to identify which code belongs to which sample when extracting several colors.
- For a one-time RGB lookup, the More Colors dialog avoids enabling macros. If you need HEX and the dialog only shows RGB, convert each pair by using a trusted color converter or use the VBA function.
- After changing a source cell’s color, force a recalculation. On Windows, press
Ctrl+Alt+F9to rebuild calculations. You can also select the formula cell, pressF2, and then pressEnterto re-enter it. Recheck the output rather than assuming formatting triggered an update. - Use the returned code in applications that accept six-digit RGB HEX notation. Some tools expect the six digits without #, or use a different color model, so follow the receiving tool’s format instructions.
Troubleshooting
The formula name is not recognized. Confirm that the code is in a standard module and that macros are enabled for this trusted workbook. Check that the workbook is open in desktop Excel, not Excel for the web. If the formula was entered before the code was added, re-enter it after saving the workbook as .xlsm.
The result is #VALUE!. Check for a single-cell reference and use only fill or font as the second argument. If you renamed the VBA function, update the worksheet formula to use the same name. Also check that the code was pasted completely, including the final End Function.
The code does not match the color visible on screen. This function reads direct cell formatting through Excel’s Interior or Font properties. Conditional formatting can override the visible fill or font, and table styles can also affect appearance. The function does not report those displayed results. For a conditional-formatting color, inspect the rule at Home > Conditional Formatting > Manage Rules and identify the color specified there. For table styles, check the table’s design options and style. Do not treat the direct-formatting result as the visible color when a rule or style is controlling it.
The result stays unchanged after recoloring the cell. Formatting changes alone may not trigger a formula recalculation. Force a full calculation with Ctrl+Alt+F9 on Windows, or edit and re-enter the formula. If the source cell uses a theme color, changing the workbook theme can also change its appearance; refresh the formula result afterward.
You cannot use the VBA method on Mac or the web. Excel for Mac desktop supports VBA, although editor access and keyboard shortcuts can vary. Excel for the web cannot run VBA functions. Open the workbook in desktop Excel, or use the More Colors dialog for an RGB lookup where available.
Frequently Asked Questions
Can Excel show a HEX color code without VBA?
Sometimes the More Colors dialog includes a HEX field, depending on the Excel build. If it does not, it can still show RGB values. The VBA function provides a worksheet result in HEX format.
What is the difference between HEX and RGB?
They represent the same red, green, and blue color components in different formats. RGB commonly uses three decimal values from 0 to 255; HEX uses six hexadecimal digits, two for each component. For example, RGB 68, 114, 196 is #4472C4.
Does the function read conditional-formatting colors?
No. It reads the cell’s direct fill or font formatting. Conditional formatting can change what you see without changing that direct formatting, so inspect the relevant rule instead.
Why does the color formula not update automatically?
Excel usually recalculates formulas when their input values change, not when formatting changes. Force a calculation or re-enter the formula after recoloring the source cell.
Can I use the function in Excel for the web?
No. Excel for the web does not run VBA user-defined functions. Use desktop Excel for the macro, or use the color dialog for RGB values if available in your version.
Can I get a HEX code for a font color?
Yes. Use =ExcelColorCode(A2,"font") with A2 replaced by the single cell whose text color you want to read. The function reports “Automatic” if Excel is choosing the font color automatically.
Related
- Inventory Spreadsheet in Google Sheets or Google Docs?
- Google Sheets Inventory Spreadsheet: Setup and Tips
- Inventory Spreadsheet: Simple Setup and Useful Tips
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Checkbox in Google Sheets: Add and Use ItTo add a checkbox in Google Sheets, select the cell or range, then choose Insert > Checkbox. Click…
- Export Outlook Contacts to PST: Steps and OptionsYes. In classic Outlook for Microsoft 365 on Windows, go to File > Open & Export > Import/Export,…
- How to Insert a Horizontal Line in a Word DocumentTo add a quick divider in Word for Microsoft 365 on Windows, type three hyphens (—) on a…
- How to Add Custom Page Numbers in WordTo add custom page numbers in Word, choose Insert > Page Number, select a position and style, then…
- How to Put a PowerPoint Slideshow on LoopTo make a PowerPoint presentation repeat, open it in the desktop app and choose Slide Show > Set…
- Compressing Pictures in PowerPoint: Settings and StepsTo compress pictures in PowerPoint for Microsoft 365 on Windows, select a picture, go to Picture Format >…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.