Macro on opening excel

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.

  1. Click Developer > Visual Basic.

    Code group on the Developer tab

  2. 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.

    ThisWorkbook module in the Visual Basic Editor (VBE)

  3. In the module window that opens on the right, insert the following code:

    Private Sub Workbook_Open()
    
    ' Put your code here
    
    End Sub

  4. 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).

  5. 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:

  1. On the menu, click Excel > Preferences… > Ribbon & Toolbar.

  2. In the Customize the Ribbon category, in the Main Tabs list, select the Developer check box.

  3. 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.

  1. Click Developer > Visual Basic.

  2. In the VBA Project Explorer on the left hand side, expand the VBA Project folder for your workbook, then double-click the ThisWorkbook module.

  3. In the module window that opens on the right, insert the following code:

    Private Sub Workbook_Open()
    
    ' Put your code here
    
    End Sub

  4. 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).

  5. 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.

Skip to content

Run a Macro Automatically on Opening Excel Workbook

Home » Excel VBA » Run a Macro Automatically on Opening Excel Workbook

  • run a macro automatically

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:

run a macro automatically

Instructions:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Double click on ThisWorkbook from Project Explorer
  4. Copy the above code and Paste in the code window
  5. Save the file as macro enabled workbook
  6. 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:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Insert a New Module from Insert Menu
  4. Copy the above code and Paste in the code window
  5. Save the file as macro enabled workbook
  6. 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:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Insert a New Module from Insert Menu
  4. Copy the above code and Paste in the code window
  5. Save the file as macro enabled workbook
  6. 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:
  1. Open an excel workbook
  2. 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
  3. 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
  4. Press Alt+F11 to open VBA Editor
  5. Double click on ThisWorkbook from Project Explorer
  6. Copy the above code and Paste in the code window
  7. Save the file as macro enabled workbook
  8. 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:
  1. Open an excel workbook
  2. Press Alt+F11 to open VBA Editor
  3. Insert a userform from Insert menu (UserForm1)
  4. Double click on ThisWorkbook from Project Explorer
  5. Copy the above code and Paste in the code window
  6. Save the file as macro enabled workbook
  7. 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:
  1. Open an excel workbook
  2. Enter some sample data in each workbook
  3. Press Alt+F11 to open VBA Editor
  4. Double click on ThisWorkbook from Project Explorer
  5. Copy the above code and Paste in the code window
  6. Save the file as macro enabled workbook
  7. 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

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

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
Excel VBA Project Management Templates
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

  1. Haobo
    December 30, 2013 at 4:32 AM — Reply

    Thank 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
    Next

    End Sub

  2. PNRao
    December 30, 2013 at 10:08 PM — Reply

    Hi 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!

  3. Fauzan
    May 15, 2014 at 12:11 PM — Reply

    Actually 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

  4. Gabor Horvath
    October 7, 2014 at 6:49 PM — Reply

    Excellent help, thanks. Gabor

  5. lokesh reddy
    July 22, 2015 at 7:40 AM — Reply

    Hi,

    I think we need explanation about coding in depth. example :dim sh, dim icntr

  6. Prabhu
    October 5, 2015 at 10:52 AM — Reply

    Hi 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

  7. RAHUL
    April 18, 2016 at 5:26 PM — Reply

    Hello, 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 ??

  8. Venkat
    May 24, 2016 at 7:46 PM — Reply

    Dear 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…

  9. gurunath
    March 10, 2017 at 10:20 AM — Reply

    Hi,
    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.

  10. Phani Ch
    March 31, 2017 at 9:35 AM — Reply

    I 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.

  11. Alaine
    September 7, 2018 at 3:10 PM — Reply

    Hi, 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.

  12. Aniket Shinde
    May 13, 2019 at 1:26 PM — Reply

    I 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

  13. algae
    June 5, 2019 at 12:24 PM — Reply

    My workbook has a macro name “Main”. How do I run it whenever the workbook is opened?

  14. PNRao
    July 4, 2019 at 5:56 PM — Reply

    Paste the below code in the workbook code module:


    Private Sub Workbook_Open()
    Call Main
    End Sub

  15. Devmode
    May 11, 2020 at 11:35 PM — Reply

    How 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

With Our Professional and Premium Project Management Templates!

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.

Analysistabs Logo

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


Automatically run Macro when opening a workbook

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:

  1. Run the macro when opening the workbook which contains the macro.
  2. 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).

Run Macro on Open VBE Editor

Double click on This Workbook from the Project Explorer window

Run Macro on Open Workbook

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 on Open Workbook Message

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

Run Macro on Any Workbook Open

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.

Run Macro on Any Workbook Open MessageRun Macro on Any Workbook Open Message

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.


Headshot Round

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:

  1. Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
  2. Ask the ‘Excel Ninja’ in your office. It’s amazing what things other people know.
  3. 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.
  4. 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

  • Read
  • Discuss
  • 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.

    load-form-when-excel-starts

    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!

    alt text

    Learn More!

    <<Return to VBA Examples

    Like this post? Please share to your friends:
  • Macro in word template
  • Macro in excel with button
  • Macro in excel not working
  • Macro in excel file
  • Macro from excel to word