logo
search
VBA & Macro Problems

How to Get an Excel Cell's Color Code Using VBA

Maira MehtabMaira Mehtab Sep 22, 2026 869 views

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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press the ALT + F11 keys simultaneously on your keyboard to launch the Visual Basic for Applications editor.

2
Insert a New Module

In the top menu bar, click on 'Insert' and select 'Module'. This will open a blank window where you can write your custom script.

3
Enter the Custom Function Code

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.

4
Use the Custom Formula

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.

Conditional Formatting Limitation: This VBA method only extracts the cell's base fill color. If the cell's color is applied via a Conditional Formatting rule, you must use DisplayFormat.Interior.Color in your VBA script instead.
WPS Spreadsheets Macro Support

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. 1. Open the Developer Tab: Launch your spreadsheet in WPS Office and navigate to the 'Developer' tab located on the top ribbon.
  2. 2. Access the Visual Basic Editor: Click on 'Visual Basic Editor' to open the macro programming environment.
  3. 3. Insert Your Code: Click 'Insert' > 'Module' and paste your custom VBA code for extracting the color hex code.
  4. 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.
Fully compatible with Microsoft Excel (.xlsx and .xlsm) formats and native VBA code.Advanced Developer tools built-in to support User Defined Functions.Lightweight architecture that runs smoothly even on older computers.Free and intuitive tabbed interface for efficient multitasking.
microsoft office alternative - wps office

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.