XL: How to Create and Use an Auto_Close Procedure

Last reviewed: February 3, 1998
Article ID: Q141563
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel for Windows 95, version 7.0
  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition

SUMMARY

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.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

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 will run.

To create an Auto_Close procedure, use the following steps:

  1. In the module sheet you want to store the procedure in, create a new procedure named Sub Auto_Close().

  2. Write the code for the procedure.

  3. 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, click the Tools menu, point to Macro, 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 the Run button.

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 to close and save workbooks.

Example

   ' Auto_Close macro to save the active workbook.

   Sub Auto_Close()
      ActiveWorkbook.Save
   End Sub

REFERENCES

For more information about Auto_Close procedures, click the Index tab in Microsoft Excel Help, type the following text

   Auto_Close Procedures

and then double-click the selected text to go to the "Running procedures when you open or close a workbook" topic.

"Microsoft Excel Visual Basic Programmer's Guide," pages 208-210

"Microsoft Excel Visual Basic User's Guide," version 5.0, pages 266-267


Additional query words: 7.00 8.0 xl97 5.00 5.00c auto close
autoclose
Keywords : kbcode kbprg PgmHowto
Version : WINDOWS:5.0,5.0c,7.0,97; MACINTOSH:5.0,5.0a,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.