Microsoft KB Archive/213639

= XL2000: How to Create and Use a Workbook_BeforeClose Procedure =

Article ID: 213639

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



This article was previously published under Q213639





SUMMARY
You can run a Microsoft Visual Basic for Applications macro (Sub procedure) every time a particular workbook is closed, by attaching the procedure to the BeforeClose event of the workbook. This article contains sample code that illustrates how to use the BeforeClose event.



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. To attach a procedure to the BeforeClose event:  Start the Visual Basic Editor (press ALT+F11) On the View menu, click Project Explorer. In the Project Explorer window, double-click the ThisWorkbook object.

The module sheet for the ThisWorkbook object is opened.  Type the following code in 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  Switch to Microsoft Excel by pressing ALT+F11. On the File menu, click Close.</li></ol>

The Workbook_BeforeClose procedure starts, which places the current date and time into Sheet1.

<div class="references_section">