Change Excel Cell Values Based on Background Color with VBA
Question details
Use VBA to change worksheet values according to cell background colors instead of using nested IF formulas or GET.CELL names.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A user wants to assign values or calculations to cells based on the background colors of other cells.
- Observed behavior
- The desired result is for a macro to check each cell's fill color and write the corresponding value to a destination cell.
Identify the source cells, their exact fill colors, and the destination cells where results should be written. The example checks manually applied fills in C2:C100 and overwrites matching cells in D2:D100, so adjust the ranges and sheet name before running it.
Use a VBA Select Case macro to map fill colors to values
Use this approach to check a fixed range of cells and write a result for each matching fill color.
The example maps pure red to 1 and pure blue to 0. It checks Interior.Color, which represents the cell's underlying fill, not necessarily the color displayed by conditional formatting. Cells with other colors leave the destination unchanged.
In Excel, open the workbook and select Developer > Visual Basic. On Windows, you can also press Alt+F11.
In the VBA editor, select Insert > Module and paste this code: Sub ChangeValuesByFillColor() Dim ws As Worksheet Dim cell As Range Set ws = ThisWorkbook.Worksheets("Sheet1") For Each cell In ws.Range("C2:C100") Select Case cell.Interior.Color Case RGB(255, 0, 0) ws.Cells(cell.Row, 4).Value = 1 Case RGB(0, 0, 255) ws.Cells(cell.Row, 4).Value = 0 End Select Next cell End Sub
In the module, replace Sheet1, C2:C100, the RGB colors, and the assigned values as needed. Change the column number 4 if results should go somewhere other than column D. You can replace 1 or 0 with an appropriate calculation.
Use File > Save As to save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Then select Developer > Macros, choose ChangeValuesByFillColor, and select Run. Enable macros only if you trust the workbook.

Work with Color-Formatted Spreadsheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface. Open compatible Excel files and continue working with cell values and formatting as you move between office suites. It does not fix Excel's VBA behavior, and macro compatibility may vary.

Frequently Asked Questions
Can the macro use ColorIndex instead of RGB colors?
Yes. Replace the Select Case expression with cell.Interior.ColorIndex and use the applicable color-index numbers in the Case lines. Verify the indexes in the workbook because they depend on its color palette.
Does Interior.Color detect conditional formatting colors?
Interior.Color checks the cell's underlying fill, not necessarily its displayed conditional formatting color. For displayed formatting, investigate the cell's DisplayFormat.Interior.Color property and test it with your workbook.
Will changing a cell's fill color run the macro automatically?
No. Select Developer > Macros and run ChangeValuesByFillColor again to update the results. Automatic updates require additional event or scheduling logic.
How can I use the macro on another worksheet?
Change ThisWorkbook.Worksheets("Sheet1") to the target sheet's name. Keep the range and destination references qualified with ws so the macro writes to that worksheet.
Why are some cells not producing a result?
The fill must match one of the RGB values exactly. Check the fill color and the source range in the VBA module; cells with unmatched colors leave their destination values unchanged.




