How to Extract Excel Data Based on Cell Color and Text using VBA
Question details
The user needs a method to extract or categorize data from an Excel spreadsheet by evaluating both the text content and the background fill color of the cells.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Categorizing or extracting specific labels based on visual formatting (fill color) and text combinations, which standard spreadsheet formulas cannot process.
- Observed behavior
- Built-in Excel formulas fail to evaluate cell fill colors reliably, meaning the extraction requires a custom VBA script or a completely redesigned data structure.
Before writing your macro, ensure you determine the exact numerical values of the background colors used in your spreadsheet, as your VBA code needs these specific integers to trigger accurately.
Use a VBA Macro to Check Cell Color and Text
A custom VBA macro is the most reliable way to read a cell's Interior.Color property alongside its text value, allowing you to accurately map text/color combinations to specific output labels.
Standard functions cannot read formatting like background colors. By utilizing VBA, you can iterate through a designated range of cells, check both their text contents and background fill codes, and output the corresponding categorical label to an adjacent column.
Select a cell with the target background color. Press Alt+F11 to open the VBA Editor, then press Ctrl+G to open the Immediate Window. Type '? ActiveCell.Interior.Color' and press Enter to reveal the color's numerical value. Repeat this for all colors you need.
In the VBA Editor, navigate to the top menu and click Insert > Module to create a blank canvas for your macro.
Write a VBA loop (e.g., 'For Each cell In Range') that iterates through your specific data roster. Inside the loop, check the current cell's value and interior color.
Use 'If...ElseIf' or 'Select Case' statements to assign outputs. For example, if 'cell.Value = "XV"' and 'cell.Interior.Color = 4626167' (an orange code), command the macro to write 'AREA1' in the adjacent offset cell.
Close the VBA editor or press F5 while inside the macro code to execute the script and automatically label your dataset.

Extract Data by Color in WPS Spreadsheets
WPS Spreadsheets includes powerful support for VBA macros, allowing you to seamlessly run scripts that extract or categorize data based on cell colors and text, completely matching the capabilities of Microsoft Excel.
- 1. Open your spreadsheet: Launch WPS Spreadsheets, open your document, and press Alt+F11 to open the built-in VBA Editor.
- 2. Identify cell colors: Press Ctrl+G to access the Immediate Window and find your exact background color integers using the '? ActiveCell.Interior.Color' command.
- 3. Create your macro: Go to Insert > Module and write your loop script to evaluate cell colors and text strings simultaneously.
- 4. Execute the extraction: Run the macro to evaluate your roster and populate the corresponding area labels automatically.

Frequently Asked Questions
Can I extract data by cell color using standard formulas instead of VBA?
No, standard built-in spreadsheet formulas do not have functions to evaluate a cell's visual formatting, including background fill colors. You must use VBA or rely on legacy Macro 4.0 functions like GET.CELL.
Why is my VBA macro not recognizing the background color?
The background color applied to the cell must exactly match the numerical color value specified in your VBA script. Additionally, if the color is dynamically applied via Conditional Formatting, 'Interior.Color' will not read it; you must use 'DisplayFormat.Interior.Color' instead.
How do I save my workbook after adding the color-extraction macro?
To preserve your VBA code for future use, you must save the document as a Macro-Enabled Workbook (.xlsm). Saving it as a standard .xlsx file will permanently strip the macro from the file.




