Run Excel VBA Code When a Cell Is Double-Clicked
Question details
Run existing invoice-update VBA code when a specific Excel cell is double-clicked instead of using a command button.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- An invoice workbook needs to increment an invoice number and update the year when the user double-clicks a designated cell.
- Observed behavior
- The desired behavior is to run the invoice-update code on a double-click without entering cell-edit mode.
Open the workbook in a desktop version of Excel that supports VBA, and identify the worksheet and cell that should trigger the code. Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) and enable macros if prompted and you trust the file.
Use the Worksheet BeforeDoubleClick event
Add a worksheet event to run invoice-update code only when the designated cell is double-clicked.
Worksheet_BeforeDoubleClick receives the clicked cell as Target. Setting Cancel to True prevents Excel from starting its normal cell-edit action.
The example uses B2 as both the trigger and invoice-number cell, and B3 as the year cell. Change these references to match your workbook.
In Excel, right-click the tab for the relevant worksheet and select View Code. This opens that worksheet's code module in the VBA editor.
In the worksheet code window, add this example: Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) If Intersect(Target, Me.Range("B2")) Is Nothing Then Exit Sub Cancel = True If Not IsNumeric(Me.Range("B2").Value) Or IsEmpty(Me.Range("B2").Value) Then MsgBox "Enter a numeric invoice number in B2 first." Exit Sub End If Me.Range("B2").Value = Me.Range("B2").Value + 1 Me.Range("B3").Value = Year(Date) End Sub
Replace the example update statements with your existing invoice logic, or call a suitable shared procedure from the event. If the worksheet already has a Worksheet_BeforeDoubleClick procedure, add the cell check and logic to that procedure rather than creating a duplicate.
Save the workbook as .xlsm, return to the worksheet, and double-click B2. Confirm that the invoice number increments, B3 shows the current year, and Excel does not enter edit mode.

Manage Invoice Spreadsheets with WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move invoice workbooks between office suites. Excel VBA event compatibility may vary, so test your macros before relying on them in WPS.

Frequently Asked Questions
Where should I put the double-click event code?
Right-click the relevant worksheet tab in Excel and select View Code. Put the Worksheet_BeforeDoubleClick procedure in that worksheet's module, not in a standard module.
How do I change the cell that triggers the macro?
In the event procedure, change Me.Range("B2") in the Intersect check to the desired cell address. Also update the invoice and year cell references if they differ.
Why does Excel enter edit mode when I double-click the cell?
Set Cancel = True inside the Worksheet_BeforeDoubleClick procedure after confirming that Target is the designated cell. This suppresses the normal double-click edit action for that cell.
Why does the event stop working after I save the workbook?
Use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm). When reopening the file, enable macros if prompted and you trust the workbook.




