VBA & Macro Problems

Change Excel Cell Values Based on Background Color with VBA

Anum MushtaqAnum Mushtaq Dec 2, 2026 868 views

Question details

Use VBA to change worksheet values according to cell background colors instead of using nested IF formulas or GET.CELL names.

Change Excel Cell Values Based on Background Color with VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, open the workbook and select Developer > Visual Basic. On Windows, you can also press Alt+F11.

2
Insert the macro

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

3
Adjust the mappings

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.

4
Save and run

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.

Use a VBA Select Case macro to map fill colors to values
Run the macro after color changes: The macro does not update values automatically when a fill color changes. Run it again to refresh the results. It overwrites existing values or formulas in destination cells when a color matches.
Free Microsoft Office alternative

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.

Use a familiar spreadsheet interface to work with cell values and fill colorsOpen and edit Microsoft Excel formats such as XLSX and XLSGet a free, lightweight alternative for everyday office workMove existing compatible spreadsheets into WPS Office without starting from scratch
microsoft office alternative - wps office

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.