Question

I have a Workbook with the following code in Sheet1:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
UserForm1.Show
End Sub

When I double click on a row, this opens my Userform but only if I'm on Sheet1.

How can get this to work for any active worksheet without adding it to every sheet?

Was it helpful?

Solution

If you want your code to be executed for the active sheet in your workbook, then you need to remove the code you have for the single sheet and do the following:

  1. Open the VBA Editor and find the ThisWorkbook object under "Microsoft Excel Objects".
  2. Edit it and add the following code inside it:

    Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
      UserForm1.Show
    End Sub
    

This means that the double-click event will be detected for all sheets in the workbook. If you leave your code in the sheet, the sheet event gets fired first and then the workbook one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top