Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later 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 May 6, 2023)
This tip applies to Excel 97, 2000, 2002, and 2003


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 order to run a procedure automatically when a workbook is opened, all you need to do is name the procedure Auto_Open(). Thus, the following procedure will be run automatically whenever the workbook containing it is opened:

Sub Auto_Open()
    Dim strMsg As String
    Dim intBoxType As Integer
    Dim strTitle As String
    Dim intUpdate As Integer
    Dim strDefault As String
    Dim strOldFile As String
    Dim intStatusState As Integer

    strMsg = "Do you want to save yesterday's transactions?"
    intBoxType = vbYesNo + vbQuestion
    strTitle = "Automatic Backup Routine"
    intUpdate = MsgBox(Msg, BoxType, Title)
    If intUpdate = vbYes Then
        strMsg = "Which filename would you like use?"
        strDefault = "OLD.DAT"
        strOldFile = InputBox(strMsg, strTitle, strDefault)
        intStatusState = Application.DisplayStatusBar
        Application.DisplayStatusBar = True
        Application.StatusBar = "Updating past months..."
        UpdateYesterday
        Application.StatusBar = False
        Application.DisplayStatusBar = intStatusState
    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()

Many people consider using Auto_Open as the "old way" of this type of macro and Workbook_Open as the "new way." In a sense that is true; the Workbook_Open method is a more object-oriented approach to this type of macro than is Auto_Open. In practice, however, there is very little difference between the two.

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 (2289) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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 Empty Rows after Importing

Import data into a worksheet (or paste it there) and you may find that you end up with a group of blank cells you need to ...

Discover More

Making Revisions

You've turned on Highlight Changes, but how do you know what has been changed? This tip explains how Excel displays those ...

Discover More

Jumping to the Real Last Cell

Jumping to the last cell in a worksheet should be easy, but you may not always get the results that you expect. This tip ...

Discover More

Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!

More ExcelTips (menu)

Noting the Workbook Creation Date

You may want to add, to your worksheet, the date on which a particular workbook was created. Excel doesn't provide a way ...

Discover More

Controlling Display of Toolbar Buttons

You can assign your macros to a series of custom toolbar buttons, but you may only want those buttons to be visible when ...

Discover More

Using InputBox to Get Data

Need your macro to get some input from a user? The standard way to do this is with the InputBox function, described in ...

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 menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.