Restore a Desk Cell's Original Color in Excel with VBA
Question details
Highlight a selected desk cell on an Excel floor plan and restore its original fill when another cell is selected.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A floor plan uses worksheet cells to represent desks, and the selected desk should be visually highlighted.
- Observed behavior
- The selected desk should return to its original color when the selection changes.
Identify the worksheet and cell range that contain the desks; the example below uses B2:D10. Make sure you can open the VBA editor and enable macros for a trusted workbook.
Use Worksheet_SelectionChange to restore the previous desk color
Use a worksheet event to remember the selected desk's fill, highlight it, and restore the fill on the next selection.
Excel does not provide a general cell-click event. Worksheet_SelectionChange runs when the selection changes on the worksheet, making it suitable for this task.
This example handles single-cell selections and restores ordinary solid fills or no fill. It does not preserve all formatting details, such as theme-based colors or patterned fills.
In Excel, press Alt+F11 to open the VBA editor. In Project Explorer, double-click the floor-plan worksheet under Microsoft Excel Objects.
Paste the following code into that worksheet's code window. Replace B2:D10 with the desk range and change the RGB values if you want a different highlight color: Option Explicit Private previousDesk As Range Private previousColor As Long Private previousColorIndex As Long Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not previousDesk Is Nothing Then If previousColorIndex = xlColorIndexNone Then previousDesk.Interior.ColorIndex = xlColorIndexNone Else previousDesk.Interior.Color = previousColor End If Set previousDesk = Nothing End If If Target.CountLarge <> 1 Then Exit Sub If Intersect(Target, Me.Range("B2:D10")) Is Nothing Then Exit Sub Set previousDesk = Target previousColor = Target.Interior.Color previousColorIndex = Target.Interior.ColorIndex Target.Interior.Color = RGB(255, 255, 0) End Sub
Choose File > Save As and save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Enable macros if prompted, select a desk cell, and then select another cell to check that the first desk's fill is restored.

Work with Floor-Plan Spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and continue everyday editing as you move between office suites. It does not fix Excel's VBA event behavior, and macro support may vary by version.

Frequently Asked Questions
Where should I paste the selection-change macro?
Press Alt+F11 and double-click the floor-plan worksheet under Microsoft Excel Objects. Paste the code into that worksheet's code window, not into a standard module.
Why does the code check for a single selected cell?
The example highlights one desk at a time. If you select multiple cells, Target.CountLarge is greater than 1, so the code restores the previous desk and exits without highlighting the selection.
Can I restore a fixed color instead of storing each desk's color?
Yes. If all desk cells have the same original fill, set previousDesk.Interior.Color to that fixed RGB color when the selection changes, rather than storing each cell's original color.
Why isn't the highlight removed when I switch worksheets?
Worksheet_SelectionChange runs when the selection changes on the floor-plan worksheet, not when you switch away from it. Select another cell on the floor-plan sheet to trigger the restoration.
Does this code restore colors set by conditional formatting?
The code stores the cell's Interior fill, not its displayed conditional-formatting result. Review the rules under Home > Conditional Formatting if a rule controls the desk's visible color.




