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:
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.
Dive Deep into Macros! Make Excel do things you thought were impossible, discover techniques you won't find anywhere else, and create powerful automated reports. Bill Jelen and Tracy Syrstad help you instantly visualize information to make it actionable. You’ll find step-by-step instructions, real-world case studies, and 50 workbooks packed with examples and solutions. Check out Microsoft Excel 2019 VBA and Macros today!
Excel allows you to define names that can refer to either ranges of cells or to constant information, such as formulas. ...
Discover MoreYou can use the Zoom feature of Excel to magnify what Excel shows of your workbook, but it affects the entire screen. ...
Discover MoreWant to have you macro completely hide the Excel interface? You can do so by using the Visible property for the Excel ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments