Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to make a macro that automatically starts when you open a workbook in Excel 2000


View products that this article applies to.

Summary

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.

↑ Back to the top


More information

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.

Example

  1. Save and close all open workbooks.
  2. Open a new workbook, and then start the Visual Basic Editor (press ALT+F11).
  3. Press CTRL+R to switch to the Project Explorer window.
  4. In the Project Explorer window, right-click the ThisWorkbook object, and click View Code on the shortcut menu.
  5. In the module, type the following code:
    Private Sub Workbook_Open()
        Msgbox Date
        Worksheets("Sheet1").Range("A1").Value = Date
    End Sub
    					
  6. Switch to Microsoft Excel and save the workbook.
  7. 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.
  8. Click OK in the message box.
Note, cell A1 on Sheet1 also contains the date as a result of the macro being run.

↑ Back to the top


References

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.

↑ Back to the top


Keywords: KB265113, kbprogramming, kbhowto, kbdtacode, kbautomation

↑ Back to the top

Article Info
Article ID : 265113
Revision : 6
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 243