OnOpenExcelWorksheet Event To Trigger When Worksheet Opens? OnOpenExcelWorksheet Event To Trigger When Worksheet Opens? vba vba

OnOpenExcelWorksheet Event To Trigger When Worksheet Opens?


You can put certain code in the Worksheet_Activate() function which will run when the sheet is selected. Additionally, use the Worksheet_Deactivate() to run code when you leave the wokrsheet and go to another one. These functions go in the worksheet object code.

Private Sub Worksheet_Activate()    MsgBox ("Hi")End SubPrivate Sub Worksheet_Deactivate()    MsgBox ("Bye")End Sub


I'm not sure how to get code to run when a worksheet opens, but you can get it to run when a workbook opens.

In the VBA editor, open the Microsoft Excel Object called "ThisWorkbook." At the top of the editor window, you should see two drop-down boxes: (General) and (Declarations). Change the (General) combo box to be "Workbook."

This will give you method called Workbook_Open(). Code placed in this method will execute when you open the Excel Workbook.


Furthermore, you have more events at your disposable, available in the (Declarations) section when you have Workbook selected, such as SheetActivate and SheetChanged, among others. I haven't used those methods, but they may be something to try if you need events related to individual worksheets and not just the entire workbook.


One way I have found to autorun while opening with Worksheet code only -without Workbook code - is with the Calculate event triggered by i.e. a formula =Now().

This can be relevant if copying the sheet to a new workbook should be allowed and the VBA code to run there as well.