Forum Discussion
Automatically run a macro when opening a workbook
- Sep 13, 2022
connderosa you need to open the VBA window (ALT-F11) and then in the project explorer (CTRL-R if it isn't already open) you expand the 'microsoft excel objects' folder and double click the "ThisWorkbook".
You should then get a blank page as shown in the image below and click the left drop down and select 'Workbook'. This should automatically insert a
Private Sub Workbook_Open()
End Sub
Then put the code you want to run between those 2 lines
and in the end make sure you save it as a .xlsm or .xlsb file type
hmmm... I should have opened that link first as it says the same thing but better. lol.
so maybe you need to explain better what your question/need is
connderosa you need to open the VBA window (ALT-F11) and then in the project explorer (CTRL-R if it isn't already open) you expand the 'microsoft excel objects' folder and double click the "ThisWorkbook".
You should then get a blank page as shown in the image below and click the left drop down and select 'Workbook'. This should automatically insert a
Private Sub Workbook_Open()
End Sub
Then put the code you want to run between those 2 lines
and in the end make sure you save it as a .xlsm or .xlsb file type
hmmm... I should have opened that link first as it says the same thing but better. lol.
so maybe you need to explain better what your question/need is
- SnowMan55Oct 29, 2022Bronze ContributorSuggestion: While you are testing your code, put a Stop statement as the first statement in your Workbook_Open event handler; save the workbook with that. The advantage to that: As soon as you open the workbook, execution will halt and the VBA editor will open. Press F5 to continue (or click the corresponding Continue button).
Also, if your code is sufficiently well-behaved (i.e., it does not rely on any module-scope of public-scope variables to be at their initial values), you do not have to close and reopen your workbook to test your code. Just place your cursor in the Workbook_Open event handler and press F5.
Remove the Stop statement when you are satisfied with your code.