You might want a macro you recorded to run automatically when you open a specific workbook. The following procedure uses an example to show you how that works. You may also want to run macros automatically when Excel starts.
Before you get started, make sure the Developer tab is shown on the ribbon. For more information, see Show the Developer tab.
To use the example below, open a new workbook.
Important: VBA code cannot be undone, so make sure to test your code on a blank workbook, or a copy of an existing workbook. If the code doesn't do what you want, you can close the workbook without saving changes.
-
Click Developer > Visual Basic.
-
In the VBA Project Explorer on the left hand side, expand the VBA Project folder for your workbook, then double-click the ThisWorkbook module. If you don't see the Project Explorer, you can go to View > Project Explorer, or press Ctrl+R.
-
In the module window that opens on the right, insert the following code:
Private Sub Workbook_Open() ' Put your code here End Sub
-
Paste your recorded code in the Sub procedure between the Sub and End Sub lines.
Close the Visual Basic Editor (you don’t have to save anything).
-
Save the workbook as an Excel Macro-Enabled Workbook (*xlsm), and close it.
The next time you open the workbook, the code you added to the Workbook_Open procedure will run automatically.
Before you get started, make sure the Developer tab is shown on the ribbon. To do that:
-
On the menu, click Excel > Preferences... > Ribbon & Toolbar.
-
In the Customize the Ribbon category, in the Main Tabs list, select the Developer check box.
-
Click Save.
To use the example below, open a new workbook.
Important: VBA code cannot be undone, so make sure to test your code on a blank workbook, or a copy of an existing workbook. If the code doesn't do what you want, you can close the workbook without saving changes.
-
Click Developer > Visual Basic.
-
In the VBA Project Explorer on the left hand side, expand the VBA Project folder for your workbook, then double-click the ThisWorkbook module.
-
In the module window that opens on the right, insert the following code:
Private Sub Workbook_Open() ' Put your code here End Sub
-
Paste your recorded code in the Sub procedure between the Sub and End Sub lines.
Close the Visual Basic Editor (you don’t have to save anything).
-
Save the workbook as an Excel Macro-Enabled Workbook (*xlsm), and close it.
The next time you open the workbook, the code you added to the Workbook_Open procedure will run automatically.
Need more help?
You can always ask an expert in the Excel Tech Community or get support in Communities.