Opening a workbook runs a macro automatically
This article explains how to set up a workbook so a macro is run every time you open the workbook. Excel Event code makes this possible and it is easier than you think.
Event code can run a macro based on a drop-down list, event code is VBA code stored in a worksheet module or workbook module. I will explain this later on in this article.
For example, you could have a macro that opens another workbook that you need, saving you time, making you happy, and feeling awesome.
What's on this page
1. Run a macro automatically when a specific workbook is opened
1.1 Create a macro
The following macro opens workbook Book1.xlsx in folder c:\temp
'Name macro Sub Macro1 'Open a given workbook Workbooks.Open ("c:\temp\Book1.xlsx") End Sub
1.2 Where to put the code?
Copy the macro code above and go to tab "Developer" on the ribbon. If it is missing search the internet for your Excel version and "Show developer tab".
Press with mouse on "Visual Basic" button to open the Visual Basic editor. Press with right mouse button on on your workbook in the Project Explorer window.
Press with mouse on "Insert" and then on "Module". This action adds a code module to your workbook.
Now paste the VBA code above to your code module.
Go back to Excel.
1.3 How to run macro
Go to tab "Developer" and press with left mouse button on the "Macro" button. The following dialog box appears:
Press with mouse on "Run" button and the workbook is opened.
1.4 Where to put event code?
Press Alt+F11 to open the VB Editor or go to tab "Developer" on the ribbon, press with left mouse button on "Visual Basic" button. Double press with left mouse button on "This Workbook", if you can't see it expand the list by press with left mouse button oning on the + sign.
Paste the following event code to the workbook module:
Private Sub Workbook_Open() Macro1 End Sub
Go back to Excel. Save the workbook with file extension *.xlsm, this is important.
Close workbook and open it again. The workbook Book1.xlsx in folder c:\temp is now automatically opened. (If it exists..)
2. Run a macro automatically when selecting a specific worksheet
This section describes how to run a macro if a specific worksheet is selected (activated). Each worksheet in a workbook has a corresponding worksheet module that you can easily access, however, put only event code in these modules.
Note, put regular macros in regular modules.
2.1 Worksheet event code
The following steps describe how to save event code to a specific worksheet.
- Press the right mouse button on the worksheet tab you want to edit, it is located at the left bottom of your Excel window.
- A pop-up menu appears, see the image above.
- Press with the left mouse button on "View Code" to open the corresponding worksheet module in Visual Basic Editor (VBE).
- Copy event code below.
- Paste to worksheet module.
'Event code that runs when the user press with left mouse button ons on a specific worksheet tab Private Sub Worksheet_Activate() 'Start macro named Macro2 Call Macro2 End Sub
2.2 Macro code
The following VBA code is a regular macro. it shows a message box containing "Hello world!". Here is how you store regular macros:
- Press Alt + F11 to open the Visual Basic Editor.
- Press the left mouse button on "Insert" on the top menu, see the image below.
- Press the left mouse button on "Module" to create a new module, they are located in the Modules folder shown in the image below.
- Copy code below.
- Paste to the module.
- Exit Visual Basic Editor.
Sub Macro2() MsgBox "Hello world!" End Sub
3. Run macro when a specific cell is selected
The image above demonstrates a macro that is run when a specific cell is selected on a specific worksheet. The example shown in the image above shows a message box containing text "Cell B2 is selected" when cell B2 is selected.
3.1 Worksheet code
The following steps describe how to save event code to a specific worksheet.
- Press the right mouse button on the worksheet tab you want to edit, it is located at the left bottom of your Excel window.
- A pop-up menu appears, see the image above.
- Press with the left mouse button on "View Code" to open the corresponding worksheet module in Visual Basic Editor (VBE).
- Copy event code below.
- Paste to worksheet module.
'Event code Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'Interesect method returns a range object of a rectangular intersection of two or more cell ranges If Not Intersect(Target, Range("B2")) Is Nothing Then 'Start macro named Macro3 Call Macro3 End If End Sub
3.2 Macro code
The following VBA code is a regular macro. it shows a message box containing "Cell B2 is selected". Here is how you store regular macros:
- Press Alt + F11 to open the Visual Basic Editor.
- Press the left mouse button on "Insert" on the top menu, see the image below.
- Press the left mouse button on "Module" to create a new module, they are located in the Modules folder shown in the image below.
- Copy code below.
- Paste to the module.
- Exit Visual Basic Editor.
Sub Macro3() MsgBox "Cell B2 is selected" End Sub
5. Select cell A1 on all sheets before you close a workbook - VBA
This post demonstrates a macro that automatically selects cell A1 on each sheet right before you close a workbook. The VBA code also moves the view so cell A1 is the upper left cell on all sheets.
This macro is different from regular macros, it is rund when something happens, Microsoft calls this an Event. An event macro has a designated name and must be placed in the sheet module or worksheet module. If your event code is not working you probably saved the code in a regular module.
You can see a list of available events in the workbook or sheet module.
The following code selects cell A1 in all visible sheets right before you close the workbook. You can save this macro in a regular module as well, however, you then need to run the macro manually.
VBA code
'This Event macro fires before you close a workbook and before the user is prompted to save changes. Private Sub Workbook_BeforeClose(Cancel As Boolean) 'Declare variables and data types Dim sht As Worksheet, csheet As Worksheet 'Don't show any changes the macro does on the screen, this will also make the macro faster. Application.ScreenUpdating = False 'Assigns object active sheet to variable csheet so we can go back to this sheet when the macro is finished. Set csheet = ActiveSheet 'Iterate through each worksheet in active workbook For Each sht In ActiveWorkbook.Worksheets 'Check if worksheet is not hidden If sht.Visible Then 'Activate sheet sht.Activate 'Select cell A1 in active worksheet Range("A1").Select 'Zoom to first cell ActiveWindow.ScrollRow = 1 ActiveWindow.ScrollColumn = 1 End If 'Contine with remaining worksheets Next sht 'Go back to the worksheet when this event started csheet.Activate 'Show all changes made to the workbook Application.ScreenUpdating = True End Sub
Where to copy the code
- Press Alt-F11 to open Visual Basic Editor
- Double press with left mouse button on ThisWorkbook in Project Explorer.
Ctrl + R opens Project Explorer.
- Copy aboveVBAa code.
- Paste to worksheet module.
- Exit visual basic editor
Recommended article
Recommended articles
Save links to your favorite macros in a personal tab on the ribbon for easy access and become more productive. […]
Macro category
Table of contents Save invoice data - VBA Invoice template with dependent drop down lists Select and view invoice - […]
This tutorial shows you how to list excel files in a specific folder and create adjacent checkboxes, using VBA. The […]
In this post I am going to show how to create a new sheet for each airplane using vba. The […]
Excel categories
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.