Excel vba macro run on open. You may want to open the workbook without triggering anything in some cases either to skip a certain code block for security reasons or for programming purposes. Close the visual basic editor you dont have to save anything. Sometimes you may need to run a macro automatically on opening excel workbook. The syntax of this event is this.
Run vba code when close or open workbook 1. In the past pre excel 97 one would name a macro autoopenand excel would automatically runexecute any. We can use workbookopen method or autoopen method to achieve this. To run macro as the workbook opens we will use workbook event workbookopen.
Private sub workbookopen your code here. If you would like to load a form or run some vba code when you open an excel workbook place your code in the thisworkbook code window and in the workbookopen sub. If you want to autorun a macro when opening a worksheet then you need to type your macro name in the worksheetactivate event and this code will run a macro when opening your excel spreadsheet. Excel has the ability to automatically runexecute any excel macro code of our choosing when opening an excel workbook.
The next time you open the workbook the code you added to the workbookopen procedure will run automatically. Similarly worksheetdeactivate event code will run your macro when closing a sheet. Save the workbook as an excel macro enabled workbook xlsm and close it. This of course is providing that the user has chosen to enable macros.
Following are the few cases where you are required to run a macro while opening your excel workbook. Workbookopen and autoopen are two predefined events for excel vba.