This article was previously published under Q265113
One way to automatically run a macro when you open a workbook is to use the Workbook_Open event in Microsoft Excel. Workbook_Open is a built-in workbook event that runs its macro code each time you manually open the workbook.
The following example uses the Workbook_Open event to run a macro when you open the workbook.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
Save and close all open workbooks.
Open a new workbook, and then start the Visual Basic Editor (press ALT+F11).
Press CTRL+R to switch to the Project Explorer window.
In the Project Explorer window, right-click the ThisWorkbook object, and click View Code on the shortcut menu.
In the module, type the following code:
Private Sub Workbook_Open() Msgbox Date Worksheets("Sheet1").Range("A1").Value = DateEnd Sub
Switch to Microsoft Excel and save the workbook.
Close and reopen the workbook.
When you open the file again, Excel runs the Workbook_Open macro, which displays today's date in a message box.
Click OK in the message box.
Note, cell A1 on Sheet1 also contains the date as a result of the macro being run.
For additional information about using auto macros and events, click the following article numbers to view the articles in the Microsoft Knowledge Base:
213624 How to create an Auto_Open macro to show the Data Form dialog box in Excel 2000
213581 XL: Code "behind" worksheet or workbook may not work
213220 XL2000: Summary of Workbook and Worksheet Application Events
211626 XL2000: EnableEvents property does not work in Automation server
For more information about how to use an auto open event, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type workbook object events in the Office Assistant or the Answer Wizard, and then click Search to view the topic.