VBA & Macro Problems

Toggle Excel Cell Values on Double-Click with VBA

Amos GikundaAmos Gikunda Nov 18, 2026 869 views

Question details

The user wants to toggle eligible cells in a specified range on Sheet2 to 50 and back to their original values with a double-click.

Toggle Excel Cell Values on Double-Click with VBA
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
Double-click cells in a yellow range on Sheet2 to change values below 50 to 50, then double-click again to restore them.
Observed behavior
Cells below 50 should toggle between 50 and their original values, while cells already above 50 should remain unchanged.
Before you start

Identify the exact address of the yellow range on Sheet2; the example uses B2:D10 and does not detect cell color. Make sure you can open the VBA editor and run trusted macros in the workbook.

Solution 1Recommended

Use the Sheet2 BeforeDoubleClick event to toggle values

Add a worksheet event that changes eligible values to 50 and remembers their original values for a second double-click.

The procedure applies only to the specified range, not to every yellow cell. It skips formulas, errors, nonnumeric values, and cells whose current value is 50 or above unless the procedure previously changed that cell.

Original values are kept in memory. They are lost when the workbook closes or VBA is reset, so a cell changed to 50 cannot be restored by this code after that point.

1
Open the worksheet module

In Excel, right-click the Sheet2 tab and select View Code. Paste the following procedure into the Sheet2 worksheet module, not a standard module: Private originalValues As Collection Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Dim addressKey As String Dim savedValue As Variant Dim hasSaved As Boolean Dim previousEvents As Boolean previousEvents = Application.EnableEvents On Error GoTo CleanUp If Target.CountLarge <> 1 Then Exit Sub If Intersect(Target, Me.Range("B2:D10")) Is Nothing Then Exit Sub Cancel = True If Target.HasFormula Or IsError(Target.Value2) Then Exit Sub If originalValues Is Nothing Then Set originalValues = New Collection addressKey = Target.Address(False, False) On Error Resume Next Err.Clear savedValue = originalValues.Item(addressKey) hasSaved = (Err.Number = 0) Err.Clear On Error GoTo CleanUp If hasSaved Then If IsNumeric(Target.Value2) Then If CDbl(Target.Value2) = 50 Then Application.EnableEvents = False Target.Value2 = savedValue originalValues.Remove addressKey GoTo CleanUp End If End If originalValues.Remove addressKey End If If IsNumeric(Target.Value2) Then If CDbl(Target.Value2) < 50 Then originalValues.Add Target.Value2, addressKey Application.EnableEvents = False Target.Value2 = 50 End If End If CleanUp: Application.EnableEvents = previousEvents End Sub

2
Set the target range

In the code, replace B2:D10 in Me.Range("B2:D10") with the address of the yellow range on Sheet2.

3
Save as a macro-enabled workbook

Choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). Enable macros when prompted, but only if you trust the workbook.

4
Test the toggle

On Sheet2, double-click a cell in the target range containing a number below 50. Double-click it again to restore the original value, and check that a cell already above 50 stays unchanged.

Use the Sheet2 BeforeDoubleClick event to toggle values
Manual edits affect restoration: If a toggled cell is manually changed from 50 before the next double-click, the saved value is discarded. The procedure then evaluates the cell's current value.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for opening and editing Excel files. It supports common Excel formats for an easy transition, though it does not fix Excel's VBA event behavior and macro compatibility can vary.

Edit spreadsheet values in a familiar worksheet interfaceOpen and save common Microsoft Excel formats, including XLSX and XLSUse a free, lightweight office suite for everyday spreadsheet workMove existing spreadsheets into a familiar Office-style workflow
microsoft office alternative - wps office

Frequently Asked Questions

Why does the code use a double-click instead of a single click?

Excel provides the worksheet BeforeDoubleClick event for this action. Setting Cancel = True prevents the usual double-click editing behavior inside the target range.

Will the code automatically find yellow cells?

No. Replace B2:D10 in Me.Range("B2:D10") with the exact address of the yellow range on Sheet2. The procedure checks the range, not the fill color.

What happens to a cell that already contains 50 or a higher value?

It stays unchanged unless the procedure previously changed that cell to 50 and still has its original value in memory.

Can I restore a toggled value after closing the workbook?

No. The example stores original values in memory only. Closing the workbook or resetting VBA clears that information.

Why does the code skip formula cells?

Writing 50 into a formula cell would replace its formula. The Target.HasFormula check leaves those cells unchanged.