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.
Run a Macro Automatically on Opening Excel Workbook
Description:
Sometimes you may need to run a macro automatically on opening excel workbook. Following are the few cases where you are required to run a macro while opening your excel workbook.
Run a Macro Automatically on Opening Excel Workbook – Solution(s):
We can use Workbook_Open() method or Auto_Open() method to achieve this.
Run a Macro Automatically – Example Cases:
Following are the list of situations where we need to run a macro automatically on opening an excel workbook.
- Show a welcome message to the user
- Run some starting scripts on opening the workbook
- Fill or populate the drop-down lists or other ActiveX Control while opening the workbook
- Activate a particular sheet while opening the workbook
- Show a user form while opening the workbook
- Clear the specific worksheets or ranges while opening the workbook
- Download and see it practically
Showing a welcome message to the user
When you open a workbook, you may want to pass some instructions to the user. Or you can show a welcome message with specific text or user name. The following code will show you how to Run a Macro Automatically.
Code:
Private Sub Workbook_Open() Msgbox "Welcome to ANALYSIS TABS" End Sub
Output:
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Double click on ThisWorkbook from Project Explorer
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Open the workbook to test it, it will Run a Macro Automatically. You should see a message box as shown above
Using Auto open method to run a macro automatically:
You can INSERT a new module and place the following code in the newly created module
Code:
Sub Auto_Open() Msgbox "Welcome to ANALYSIS TABS" End Sub
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a New Module from Insert Menu
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Open the workbook to test it, it will Run a Macro Automatically. You should see a message box as shown above
Running some starting scripts on opening the workbook
The following example runs a script to count the number of worksheets in workbook and list out them in the sheet1.
Code:
Sub Auto_Open() Dim sh Dim iCntr iCntr = 1 For Each sh In ThisWorkbook.Sheets Sheet1.Cells(iCntr, 1) = sh.Name iCntr = iCntr + 1 Next End Sub
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a New Module from Insert Menu
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Open the workbook to test it, it will Run a Macro Automatically. You should see the list of sheet names in the Sheet1
You may use following code to Populate a Combo Box or a List Box in worksheet
The following example shows how to populate regions(East,West,North,South) in a ComboBox and a List Box
Code:
Sub Auto_Open() Sheet1.ComboBox1.AddItem "East" Sheet1.ComboBox1.AddItem "West" Sheet1.ComboBox1.AddItem "North" Sheet1.ComboBox1.AddItem "South" With Sheets("Sheet1").ListBox1 .AddItem "East" .AddItem "West" .AddItem "North" .AddItem "South" End With End Sub
Instructions:
- Open an excel workbook
- Insert a Combobox (activex control from developer ribbon) in the Sheet1. And Name the Combo Box (right click on it and change the name in the properties) as ComboBox1
- Insert a Listbox (activex control from developer ribbon) in the Sheet1. And Name the List Box (right click on it and change the name in the properties) as Listbox1
- Press Alt+F11 to open VBA Editor
- Double click on ThisWorkbook from Project Explorer
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Open the workbook to test it, it will Run a Macro Automatically. You should see the Combo Box and List Box in the Sheet1 are filled with items
You may use following code to Activate a Sheet or Show an UserForm
The following example shows to activate a sheet (named “Home”) and show an userform (UserForm1). This code will activate the “Home” worksheet and then display the UserForm1
Code:
Sub Auto_Open() 'Activate a Sheet Sheets("Home").Activate 'Show an UserForm UserForm1.Show End Sub
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a userform from Insert menu (UserForm1)
- Double click on ThisWorkbook from Project Explorer
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Open the workbook to test it, it will Run a Macro Automatically. You should see the Userform which you have created
You may want to clear the specific worksheets or ranges while opening the workbook
The following example clears the all worksheets in the workbook on workbook open.
Code:
Sub Auto_Open() Dim sh For Each sh In ThisWorkbook.Sheets sh .Cells.Clear Next End Sub
Instructions:
- Open an excel workbook
- Enter some sample data in each workbook
- Press Alt+F11 to open VBA Editor
- Double click on ThisWorkbook from Project Explorer
- Copy the above code and Paste in the code window
- Save the file as macro enabled workbook
- Open the workbook to test it, it will Run a Macro Automatically. You should see all the worksheets are cleared
You can download the example file and see how it’s working.
Please note: We may comment have commented some code as we can write only one auto_open() or Workbook_open() procedure.
ANALYSISTABS – Run a Macro Automatically
A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.
Save Up to 85% LIMITED TIME OFFER
All-in-One Pack
120+ Project Management Templates
Essential Pack
50+ Project Management Templates
Excel Pack
50+ Excel PM Templates
PowerPoint Pack
50+ Excel PM Templates
MS Word Pack
25+ Word PM Templates
Ultimate Project Management Template
Ultimate Resource Management Template
Project Portfolio Management Templates
Related Posts
-
- Description:
- Run a Macro Automatically on Opening Excel Workbook – Solution(s):
- Run a Macro Automatically – Example Cases:
VBA Reference
Effortlessly
Manage Your Projects
120+ Project Management Templates
Seamlessly manage your projects with our powerful & multi-purpose templates for project management.
120+ PM Templates Includes:
15 Comments
-
Haobo
December 30, 2013 at 4:32 AM — ReplyThank you for the examples. Just wonder that after Dim syntax, should you declare the data type?
Sub Auto_Open()Dim sh as worksheet
For Each sh In ThisWorkbook.Sheets
sh .Cells.Clear
NextEnd Sub
-
PNRao
December 30, 2013 at 10:08 PM — ReplyHi Haobo,
Thanks for your comments.
Yes, it is good practice to declare the data type of the variable always.However, Excel can automatically define internally based on the data assigned to that particular variable in the program.Thanks-PNRao!
-
Fauzan
May 15, 2014 at 12:11 PM — ReplyActually I want to create a macro but didnt have the information of VBA to create that.
Could some one plz help me!!!!!!!!!!!
I want that if any mail comes to me with a subject of completed so i want a reminder should remind me within 5 mins and it should go on for a day as I am continuosly receiving this type of emails
-
Gabor Horvath
October 7, 2014 at 6:49 PM — ReplyExcellent help, thanks. Gabor
-
lokesh reddy
July 22, 2015 at 7:40 AM — ReplyHi,
I think we need explanation about coding in depth. example :dim sh, dim icntr
-
Prabhu
October 5, 2015 at 10:52 AM — ReplyHi I have a macro file,it wil automatically run wen the excel open up but sometime the code is not working the macro is not automatically run,plsss somebody help
anyone know what is the issue in this
-
RAHUL
April 18, 2016 at 5:26 PM — ReplyHello, I want that with out opening the workbook the macros will run by taking the system date and it will send the mail .
Can anybody help me on this ?? -
Venkat
May 24, 2016 at 7:46 PM — ReplyDear Mr.P.N Rao ..
Hello !
1. Can a macro be enabled automatically with a code inside sub auto_open() to avoid the prompt by macro security level settings prompting the user to press enable macro to run the auto_open() macro ??..Thanks in advance… -
gurunath
March 10, 2017 at 10:20 AM — ReplyHi,
I want to create one macro, in one Excel file more than 30 sheets are there. when ever i opens excel file it should open home sheet and also while opening o dont want to show all other sheets list.any one help me on this.
-
Phani Ch
March 31, 2017 at 9:35 AM — ReplyI have written a macro for my department. Now my requirement is when we generate the excel from the application, Macro should automatically run by cross checking the Worksheet Name.
Eg:- “DailyDetails”. If the generated sheet has a name like “DailyDetails_31/3/2017” it should run automatically without any shortcuts and if the generated sheet name is not as per criteria it should not run the macro. -
Alaine
September 7, 2018 at 3:10 PM — ReplyHi, i would want to ask, how can i automatically run macros on a hyperlink form upon opening an excel file?
I’m thinking of setting a specific date in outlook where it would open an excel file then run the macro automatically, but i don’t know how to call a specific hyperlink macro.
hope you can help me.
-
Aniket Shinde
May 13, 2019 at 1:26 PM — ReplyI Have a Data of Total Employees, Need to create VBA code on Employee Code. When I click on “Generate” need to show Total details of That seprate Employee.
Please help with Code
-
algae
June 5, 2019 at 12:24 PM — ReplyMy workbook has a macro name “Main”. How do I run it whenever the workbook is opened?
-
PNRao
July 4, 2019 at 5:56 PM — ReplyPaste the below code in the workbook code module:
Private Sub Workbook_Open()
Call Main
End Sub
-
Devmode
May 11, 2020 at 11:35 PM — ReplyHow would I make it auto-run a macro ONLY when I open documents from a particular folder on my desktop?
Effectively Manage Your
Projects and Resources
ANALYSISTABS.COM provides free and premium project management tools, templates and dashboards for effectively managing the projects and analyzing the data.
We’re a crew of professionals expertise in Excel VBA, Business Analysis, Project Management. We’re Sharing our map to Project success with innovative tools, templates, tutorials and tips.
Project Management
Excel VBA
Download Free Excel 2007, 2010, 2013 Add-in for Creating Innovative Dashboards, Tools for Data Mining, Analysis, Visualization. Learn VBA for MS Excel, Word, PowerPoint, Access, Outlook to develop applications for retail, insurance, banking, finance, telecom, healthcare domains.
Page load link
Go to Top
Get our FREE VBA eBook of the 30 most useful Excel VBA macros.
Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.
Claim your free eBook
There are a lot of circumstances where you need a Macro to run when a workbook is opened. It could be one of the following:
- Show a user form which requires input
- Activate a specific range or object
- To run a process which updates the data
- Check the username of the person accessing the file
- Automatically update cell values
- Show an information message or instructions
Whilst this list contains some of the most common reasons, there are almost an endless number of circumstances where this would be useful.
Within this area there are two different concepts:
- Run the macro when opening the workbook which contains the macro.
- Run a macro when any workbook is opened.
Whilst these may sound similar the process required is slightly different.
Note: You may need to enable the Developer Ribbon to follow this tutorial.
This is the easier of the two circumstances.
Open the Visual Basic Editor (Short Cut: Alt+F11).
Double click on This Workbook from the Project Explorer window
Enter the code below into the code window.
Private Sub Workbook_Open() MsgBox "You have just opened " & ThisWorkbook.Name End Sub
Save the file as a Macro-enabled workbook (with a .xlsm file extension), and close the file.
Open the workbook again – ta-dah!!! The Macro should run automatically.
Run Macro when any workbook is opened
For this code to work it is necessary to create a variable, which constantly monitors the Excel application. The monitoring of the application starts as soon as the workbook is opened, which occurs it does so in the background. Nothing will happen until you open another workbook.
Open the Visual Basic Editor (Short Cut: Alt+F11) – as shown in the above example.
Double click on This Workbook from the Project Explorer window
Enter the code sections below into the code window.
'Declare the application event variable
Public WithEvents MonitorApp As Application
'Set the event variable be the Excel Application Private Sub Workbook_Open() Set MonitorApp = Application End Sub
'This Macro will run whenever an Excel Workbooks is opened Private Sub MonitorApp_WorkbookOpen(ByVal Wb As Workbook) MsgBox "You just opened " & Wb.Name End Sub
Save the file as a Macro-enabled workbook (with a .xlsm file extension), and close the file.
Open the workbook. It may seem like nothing is different to the first example above, but it is, you just can’t see it . . . yet. Open some more workbooks. You will see that this macro runs every time a workbook is opened.
If we open an Excel file without our macro file open nothing will happen. However, if the macro file is open first it will then trigger the macro each time a workbook is opened. If you wish to constantly monitor files being opened it is a good idea to include this macro within your Personal Macro book, as this file is open first when the Excel application is launched.
About the author
Hey, I’m Mark, and I run Excel Off The Grid.
My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn’t until I was 35 that my journey really began.
In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).
Do you need help adapting this post to your needs?
I’m guessing the examples in this post don’t exactly match your situation. We all use Excel differently, so it’s impossible to write a post that will meet everybody’s needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.
But, if you’re still struggling you should:
- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
- Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it’s clear and concise. List all the things you’ve tried, and provide screenshots, code segments and example workbooks.
- Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.
What next?
Don’t go yet, there is plenty more to learn on Excel Off The Grid. Check out the latest posts:
Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article
To run a macro automatically when the workbook opens one must enable the developer’s tools in Microsoft excel.
Steps to enable the developer’s tool:
Step 1: Go to File > Options > Customize Ribbon
Step 2: Then checkmark the Developer’s tool option in the customize ribbon option.
Now you can record Macro, run Macro and do everything that a developer can do.
Steps to run macro automatically when the workbook opens:
Step 1: Go to the developer’s menu and then go to the visual basic.
Step 2: Go to ThisWorkbook Tab.
Step 3: Write down Private Sub Workbook_open() and hit enter. You will then see the below screen.
You can write your code or basically whatever you want between this and it will automatically run every time the workbook is opened.
Let us make a Welcome Message Box:
Step 4: Save the workbook as Excel Macro-Enabled Workbook.
Note: If you don’t save it as an Excel macro-enabled workbook then it will not save the macros and it will simply save as a normal excel file.
Output:
Now, whenever you open this workbook you will see this message box pops up automatically.
Like Article
Save Article
Return to VBA Code Examples
In this Article
- Workbook_Open Event
- Auto_Open
- Create and Name New Worksheet Everytime Excel Opens
- Set the Default Sheet When Workbook Opens
- Load Form Every Time Workbook Opens
- VBA Coding Made Easy
Do you need to run a macro when Excel starts? You have two options:
1. Create a Workbook_Open() sub within ‘ThisWorkbook’.
2. Place a Auto_Open() sub within any module.
Workbook_Open Event
Create a sub titled ‘Workbook_Open’ within ‘ThisWorkbook’
Workbook_open ()
MsgBox "This code ran at Excel start!"
End Sub
Auto_Open
Using the second method: Simply create a subroutine called Auto_Open and place code in it, or call another sub from there. Automatically your code runs when Excel starts.
Private Sub Auto_Open()
MsgBox "This code ran at Excel start!"
End Sub
Create and Name New Worksheet Everytime Excel Opens
The following code works opening a workbook. It automatically adds a new sheet and labels it with the date. It also checks to see that the sheet doesn’t already exist – to allow for the possibility of it being opened more than once a day.
This code makes use of the Workbook Open Event and must be placed in the workbook module under the “Open work Book” event. The function Sheet_Exist must be placed in a module and this checks whether or not the sheet exists:
Private Sub Workbook_Open()
Dim New_Sheet_Name As String
New_Sheet_Name = Format(Now(), "dd-mm-yy")
If Sheet_Exists(New_Sheet_Name) = False Then
With Workbook
Worksheets.Add().Name = New_Sheet_Name
End With
End If
Save
End Sub
Function Sheet_Exists(WorkSheet_Name As String) As Boolean
Dim Work_sheet As Worksheet
Sheet_Exists = False
For Each Work_sheet In ThisWorkbook.Worksheets
If Work_sheet.Name = WorkSheet_Name Then
Sheet_Exists = True
End If
Next
End Function
To download the .XLSM file for this tutorial, click here
Set the Default Sheet When Workbook Opens
Do you want to make sure a sheet always shows first when a workbook opens? For instance when you open a workbook sheet3 is always the active sheet. Here’s how.
You can refer to a sheet from VBA by it’s program name (ie Sheet3) or by it’s tab name(ie JanData). It is best to use the program name, becuase if the tab name changes, your VBA code that refers to a tab name will no longer work. However if you use the program name a user can change the tab name multiple times and your macro still works.
To make sure a certain sheet is always activated when a workbook opens, just place sheet.activate code in the workbook_open sub. This is an example that activates sheet3 by using the program name everytime a workbook opens.
Private Sub Workbook_Open()
Sheet3.Activate
End Sub
And this does it by using the tab name:
Private Sub Workbook_Open()
Sheets("mytabname").Activate
End Sub
Sidenote: You must save and restart excel for this to work.
Sidenote: This only works if macros are enabled.
Sidenote: Put this code in the code window for the ThisWorkbook object in the VBE.
Load Form Every Time Workbook Opens
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 Workbook_Open sub.
From your spreadsheet:
1. Press ALT and F11 to open the VB editor
2. Double-click the word ThisWorkbook to open the code window
3. Type the following code in the ThisWorkbook code window
Private Sub Workbook_Open()
UserForm1.Show
End Sub
Sidenote: Replace Userform1 with your form name
4. Close Excel and re-open.
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Learn More!
<<Return to VBA Examples