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.
The
Workbook_BeforeClose procedure starts, which places the current date and time into Sheet1.