XL98: How to Create and Use a Workbook_BeforeClose Procedure

ID: Q193663

The information in this article applies to:

SUMMARY

If you want to run a Visual Basic for Applications macro every time a particular workbook is closed, you can attach a Visual Basic procedure to the BeforeClose event of the workbook. This article contains an example that illustrates how to use the BeforeClose event.

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 professionals 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/

The following example illustrates how to use the BeforeClose event. In this example, a cell is updated with the current date and time when the workbook is closed.

1. Save and close any open workbooks and then open a new workbook.

2. Start the Visual Basic Editor by pressing OPTION+F11.

3. On the View menu, click Project Explorer.

4. Double-click the ThisWorkbook object in the Project Explorer window.

   The module sheet for the ThisWorkbook object is opened.

5. Type the following code into the module sheet:

   Private Sub Workbook_BeforeClose(Cancel As Boolean)

      ' Place the current date and time into cell A1 of Sheet1.
      Worksheets("sheet1").Range("a1").Value = _
         Format(Date + Time, "mm/dd/yy hh:mm")

      ' Save the current workbook.
      ThisWorkbook.Save
   End Sub

6. Switch to Microsoft Excel by pressing OPTION+F11.

7. On the File menu, click Save As. Type the name for your workbook in the

   Save As box, and then click OK.

8. On the File menu, click Close.

The Workbook_BeforeClose procedure will start, and the current data and time will be placed into Sheet1.

For additional information about using Auto_Close procedures in earlier versions of Microsoft Excel, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q141563
   TITLE     : XL: How to Create and Use an Auto_Close Procedure

REFERENCES

For more information about using the BeforeClose event, from the Visual Basic Editor, click the Office Assistant, type "beforeclose," click Search, and then click to view "BeforeClose Event."

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If the Assistant is not able to answer your query, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176476
   TITLE     : OFF: Office Assistant Not Answering Visual Basic Questions

Additional query words: XL98
Keywords          : kbdta kbdtacode xlvbahowto 
Version           : MACINTOSH:98
Platform          : MACINTOSH
Issue type        : kbhowto

Last Reviewed: May 18, 1999