Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 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.
by Allen Wyatt
(last updated October 9, 2017)
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()
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 Office 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.
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!
Want to get rid of all the zero values in a range of cells? This tip provides a couple of different ways you can ...Discover More
Do your macros seem to be disabled on your new machine? It could be because of the security settings in Excel. Here's ...Discover More
Macros give you a great deal of control over creating, finding, renaming, and deleting files. This tip focuses on this ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.