VBA & Macro Problems

Restore a Desk Cell's Original Color in Excel with VBA

Chanuka GeekiyanageChanuka Geekiyanage Nov 18, 2026 870 views

Question details

Highlight a selected desk cell on an Excel floor plan and restore its original fill when another cell is selected.

Restore a Desk Cell's Original Color in Excel with VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the worksheet code

In Excel, press Alt+F11 to open the VBA editor. In Project Explorer, double-click the floor-plan worksheet under Microsoft Excel Objects.

2
Add the event code

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

3
Save and test the workbook

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.

Use Worksheet_SelectionChange to restore the previous desk color
Selection changes on this worksheet: The color is restored when the selection changes on the floor-plan worksheet. Switching directly to another worksheet does not itself trigger this worksheet's SelectionChange event.
Free Microsoft Office alternative

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.

Edit and format floor-plan cells in WPS Spreadsheets.Open and save common Microsoft Excel formats, including .xlsx.Use a familiar spreadsheet interface for easier migration.Access a free, lightweight office suite for everyday work.
microsoft office alternative - wps office

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.