Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Running a Procedure when a Workbook is Opened.

Running a Procedure when a Workbook is Opened

Written by Allen Wyatt (last updated January 30, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


You can cause Excel to run a procedure automatically whenever a particular workbook is opened. For instance, when the workbook is opened, you might want to run a procedure that asks the users if they want to perform some task, such as saving the previous day's data to another file.

In reality, Excel provides two different ways you can run a procedure when a workbook is opened. In both cases, all you need to do is provide a special name for the macro; that is the clue that tells Excel you want to run the procedure when opening. You can, if desired, name the procedure Auto_Open and place it in a regular macro module. You can also define a procedure called Workbook_Open within the ThisWorkbook object.

As an example, consider the following code:

Sub Auto_Open()
    Dim sMsg As String
    Dim iBoxType As Integer
    Dim iUpdate As Integer
    Dim sDefault As String
    Dim sOldFile As String
    Dim iStatusState As Integer

    sMsg = "Do you want to save yesterday's transactions?"
    iBoxType = vbYesNo + vbQuestion
    iUpdate = MsgBox(sMsg, iBoxType, "Automatic Backup")
    If iUpdate = vbYes Then
        sMsg = "Which filename would you like use?"
        sDefault = "OLD.DAT"
        sOldFile = InputBox(sMsg, "Automatic Backup", sDefault)
        iStatusState = Application.DisplayStatusBar
        Application.DisplayStatusBar = True
        Application.StatusBar = "Updating past months..."
        UpdateYesterday(sOldFile)
        Application.StatusBar = False
        Application.DisplayStatusBar = iStatusState
    End If
End Sub

(Remember that this procedure is an example; it won't run properly on your system because it calls a function called UpdateYesterday, which does the actual updating.)

This macro runs automatically whenever the workbook to which it is attached is opened. You could also modify the code and place it within the ThisWorkbook object simply by changing the first line to this:

Private Sub Workbook_Open()

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (8451) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Running a Procedure when a Workbook is Opened.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Getting Rid of 'Mail To:' in E-mail Links

If you convert e-mail addresses to hyperlinks, you could end up with some 'mailto:' verbiage at the beginning of the ...

Discover More

Inserting Multiple Graphics in a Document

Word allows you to easily place graphics in a document. Placing one or two graphics is easy, but placing many graphics in ...

Discover More

Turning Off Aero

Windows implements a lot of visual effects that can make your screen and desktop look gorgeous. These good looks come at ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Self-Deleting Macros

Macros are very powerful, but you may not want them to always be available to a user. Here are some ways you can limit ...

Discover More

Inserting Worksheet Values with a Macro

Macros are often used to process information in a worksheet. You may need your macro to change the values stored in ...

Discover More

Macro Fails after Filter

When developing a macro that others may use, you might want to test it out to make sure it works properly if a filter is ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 - 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.