How to Get an Excel Cell's Color Code Using VBA
Question details
The user wants a straightforward method or formula to extract the fill color code of a specific cell and output it as a standard hexadecimal value (e.g., #FFFFFF).
- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Attempting to identify and extract the exact background color used in a spreadsheet cell to maintain design consistency or match data attributes.
- Observed behavior
- Excel does not offer a native worksheet formula that directly reads cell formatting or returns a background color's hex code.
Ensure that the Developer tab is enabled in your Excel ribbon, as you will need to access the Visual Basic for Applications (VBA) editor to create a custom formula.
Use a Custom VBA Macro to Extract the Hex Color Code
By creating a User Defined Function (UDF) in VBA, you can build a custom formula that reads a cell's RGB property and converts it into a standard Hexadecimal string.
Because standard Excel functions are strictly designed to read cell values rather than formatting, you must rely on a VBA script to pull the metadata of the cell's background color.
This method creates a custom function that you can type directly into your worksheet just like a native Excel formula.
Press the ALT + F11 keys simultaneously on your keyboard to launch the Visual Basic for Applications editor.
In the top menu bar, click on 'Insert' and select 'Module'. This will open a blank window where you can write your custom script.
Define a custom function (e.g., Function GetHexColor(rng As Range) As String) that evaluates the rng.Interior.Color property, converts the resulting decimal color into RGB components, and formats them into a Hex string.
Close the VBA editor and return to your Excel worksheet. Click on an empty cell and type your new formula, for example, =GetHexColor(A1), to retrieve the hex code of cell A1.
Extract Cell Colors Seamlessly in WPS Spreadsheets
WPS Spreadsheets provides a robust Developer environment with full support for VBA and macros. You can easily write and run custom scripts to extract cell color codes just as you would in Microsoft Excel, with identical syntax and excellent file compatibility.
- 1. Open the Developer Tab: Launch your spreadsheet in WPS Office and navigate to the 'Developer' tab located on the top ribbon.
- 2. Access the Visual Basic Editor: Click on 'Visual Basic Editor' to open the macro programming environment.
- 3. Insert Your Code: Click 'Insert' > 'Module' and paste your custom VBA code for extracting the color hex code.
- 4. Apply in Your Worksheet: Go back to your WPS spreadsheet grid and type your newly created formula (e.g., =GetHexColor(A1)) directly into any cell.

Frequently Asked Questions
Why doesn't Excel have a default formula for cell color?
Excel's built-in formulas are engineered to calculate data, text, and numerical values, not to read the cosmetic formatting or metadata of a cell. Reading formatting properties requires external scripting like VBA or Office Scripts.
How can I find a cell's hex code manually without writing code?
You can find it by selecting the target cell, clicking the 'Fill Color' bucket icon on the Home tab, choosing 'More Colors...', and checking the 'Hex' box located at the bottom of the Custom tab.
Will my custom VBA formula update automatically if I change the cell color?
No. Changing a cell's background color does not trigger a calculation event in Excel. You will need to manually force the worksheet to recalculate by pressing F9 to update the hex code result.
Can I extract the color code using Conditional Formatting?
Standard VBA checking the Interior.Color property will not work for Conditional Formatting. You must use VBA to evaluate the DisplayFormat.Interior.Color property to read the active color generated by a formatting rule.




