Handle Worksheet Double-Click Events in a New Excel Workbook with VBA
Question details
The user wants to handle worksheet events in a newly created Excel workbook without manually adding event code to its worksheet module.

- Product
- Microsoft Excel VBA
- Device & OS
- not provided
- Scenario
- An existing workbook creates another workbook and needs to respond when a user double-clicks a cell in the generated workbook.
- Observed behavior
- The desired result is automatic event handling in the generated workbook without requiring programmatic access to its VBA project.
Save the original workbook as an Excel Macro-Enabled Workbook (.xlsm), and allow macros only if you trust the file. Keep that workbook open while testing, because it will host the application-level event handler.
Handle the generated workbook's double-click events from the original workbook
Use an application-level event when the original workbook can remain open and you only need to respond to double-clicks in the generated workbook.
A WithEvents Application variable in the original workbook's ThisWorkbook module can capture worksheet events across open workbooks. Filtering by the generated workbook keeps the handler from affecting unrelated files.
This approach does not insert code into the new workbook or require the Trust access to the VBA project object model setting. The generated workbook does not gain a standalone event handler; the original workbook must remain open with its event code active.
In Excel, open the original .xlsm workbook, then select Developer > Visual Basic. In Project Explorer, expand that workbook's Microsoft Excel Objects folder and double-click ThisWorkbook.
Paste the following code into ThisWorkbook. If that module already contains Workbook_Open, add Set App = Application to the existing procedure rather than creating a second Workbook_Open procedure. Option Explicit Private WithEvents App As Application Private GeneratedWorkbook As Workbook Private Sub Workbook_Open() Set App = Application End Sub Public Sub CreateEventWorkbook() Set App = Application Set GeneratedWorkbook = Application.Workbooks.Add End Sub Private Sub App_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean) If GeneratedWorkbook Is Nothing Then Exit Sub If Not (Sh.Parent Is GeneratedWorkbook) Then Exit Sub Cancel = True MsgBox "Double-clicked " & Sh.Name & "!" & Target.Address(False, False) End Sub
With the original workbook's ThisWorkbook module active in the VBA editor, select View > Immediate Window. Type ThisWorkbook.CreateEventWorkbook and press Enter. This initializes the event connection and creates the workbook that the handler will monitor.
Return to Excel and double-click a cell in the newly created workbook. A message should show the worksheet name and cell address. Replace the MsgBox line with your automation code; keep Cancel = True to suppress Excel's normal double-click action, or remove that line if you want the normal action to continue.
Use WPS Spreadsheet for everyday work with generated Excel files
WPS Office is a free, lightweight alternative for opening and editing generated spreadsheets, with Microsoft Office format compatibility and a familiar interface that makes migration straightforward. It does not fix Excel's VBA event setup; use Excel for the application-level event workflow described above.

Frequently Asked Questions
Do I need to enable Trust access to the VBA project object model?
No, not for the application-level event approach. It handles events without editing another workbook's VBA project. Programmatically inserting code into a worksheet module generally requires that separate security permission, which should not be enabled just to use this solution.
Why doesn't the handler run when I double-click a cell?
Make sure the original .xlsm workbook is still open and its macros are allowed. Create the test workbook by running ThisWorkbook.CreateEventWorkbook in the VBA editor's Immediate Window. If events were disabled by other code, enter Application.EnableEvents = True in the Immediate Window. A VBA project reset also clears the event connection, so rerun the creation procedure after a reset.
Will the generated workbook keep handling events after I close the original workbook?
No. The WithEvents variable and event procedure belong to the original workbook. For independent behavior, use a macro-enabled template containing the worksheet event code and create the new workbook from that template, rather than relying on the original workbook's application event handler.
Can I monitor every open workbook instead of just the generated one?
Yes. In ThisWorkbook, remove the GeneratedWorkbook checks from App_SheetBeforeDoubleClick. The handler will then receive worksheet double-click events across open workbooks while the application event connection remains active. Add appropriate workbook or worksheet filters before running actions that should not affect unrelated files.
Does the newly created workbook have to be saved as .xlsm?
Not for this approach, because the event code stays in the original workbook. You can save the generated workbook as .xlsx if it contains no VBA code. The original workbook must remain macro-enabled and open for the handler to work.




