This article was previously published under Q141563
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
An Auto_Close procedure is an automatic procedure stored in a workbook that runs automatically when the workbook is closed. You can place automatic procedures in any Microsoft Visual Basic for Applications module in the workbook. A workbook-level automatic procedure is identified by its name (that is, Auto_Close or Auto_Open) as it appears in the module.
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. To create an Auto_Close procedure, name the procedure "Sub Auto_Close()" (without the quotation marks). You can only have one Auto_Close procedure per workbook. If you have multiple Auto_Close macros in a workbook, none of them run.
To create an Auto_Close procedure:
In the module sheet that you want to store the procedure in, create a new procedure named Sub Auto_Close().
Write the code for the procedure.
End the procedure with the code "End Sub" (without the quotation marks).
To test an Auto_Close procedure without closing the workbook in Microsoft Excel 97, point to Macro on the Tools menu, and click Macros. Then, click Auto_Close and click Run. To test the procedure in earlier versions, click Macro on the Tools menu, select the Auto_Close macro, and click Run.
Auto_Close procedures run just before the workbook closes. You may want to use this type of procedure to restore menu bars or toolbars, or toclose and save workbooks.
' Auto_Close macro to save the active workbook.Sub Auto_Close() ActiveWorkbook.SaveEnd Sub
For more information about Auto_Close procedures, click the Index tab in Microsoft Excel Help, type the following text
and then double-click the selected text to go to the "Runningprocedures when you open or close a workbook" topic.