title: VBA Excel action when closing workbook
body: <p>Is it possible automatically let Excel do an action when someone closes the file?</p>
  
  <p>Situation:
  I have an Excel file which is also used by several other people. This file publishes a mhtml file whens saving. This mhtml file will be saved with the date of yesterday like "Dashboard 2015-01-12". The seen data in the mhtml file has to contain the date related to the file name. The data seen depends on a single cell in this excel file, G2.</p>
  
  <p>I want the Excel file to do this: 
  change a single cell (G2) into the date of yesterday. Then save it. Then close it.</p>
  
  <p>I want this action to be done:
  when someone is closing the file.</p>
  
  <p>Code I have so far:</p>
  
  <pre>Sub sbWriteCellWhenClosing()
  
  Workbooks("BOOK1.XLS").Close SaveChanges:=True
  Range("G2") = Format(Now - 2, dd - mm - yy)
  
  End Sub</pre>
  
  <p>Edit:</p>
  
  <p>Will this do the job?</p>
  
  <pre>
  Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Range("G2") = Format(Now - 1, dd - mm - yy)
     ActiveWorkbook.Close SaveChanges:=True
  End Sub
  </pre>