title: VBA - Event inside event (BeforeClose -> BeforeSave) not working
body: <p>I've hijacked saving in an excel file for the purposes of preventing unintended overwrites of a file in an odd environment. </p>
  
  <p>The Workbook_BeforeSave and the Workbook_BeforeClose events work perfectly on their own. Unfortunately, the way the code is currently structured, I would need to call the BeforeSave event from within the BeforeClose event.</p>
  
  <p>In the most basic form, the following code will not do as I'd like. In the following example, wksHiddenWorksheet.Visible = True will not make wksHiddenWorksheet visible if the save is called from within the BeforeClose event.</p>
  
  <pre><code>Option Explicit
  
  Private Sub Workbook_BeforeClose(Cancel As Boolean)
  
      ThisWorkbook.Save
  
  End Sub
  
  Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  
      wksHiddenWorksheet.Visible = True
  
  End Sub
  </code></pre>
  
  <p>Is there any way around this? Is it poor practise (or even fundamentally incorrect) to call an event from inside another event in the way that I'm attempting? </p>
  
  <p>Update:</p>
  
  <p>I have played around with Application.EnableEvents in the workbook, but I've made sure it always reverts back to True in error handling.</p>
  
  <p>I've opened a brand new workbook and entered the following code</p>
  
  <pre><code>Option Explicit
  
  Private Sub Workbook_BeforeClose(Cancel As Boolean)
  
      Debug.Print Application.EnableEvents 'Prints TRUE    
      ThisWorkbook.Save
  
  End Sub
  
  Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  
      'The workbook consists of two sheets. Sheet1 and Sheet2
      Sheet1.Visible = xlSheetHidden
  
      Debug.Print Sheet1.Visible 'Prints -1 (xlSheetVisible)
  
  End Sub
  </code></pre>
  
  <p>The Visible sheet does not become hidden, though the line is triggered if I go line by line through the code. If I enter code such as a MsgBox in the same space, the MsgBox will open as normal. </p>
  
  <p>Update 2:</p>
  
  <p>To answer everyone's questions and display exactly what happens, please see the below code:</p>
  
  <pre><code>Option Explicit
  
  Private Sub Workbook_BeforeClose(Cancel As Boolean)
  
      'The workbook consists of two sheets. Sheet1 and Sheet2
      Debug.Print Application.EnableEvents 'Prints TRUE
      Debug.Print Sheet1.Visible 'Prints -1 (xlSheetVisible)
  
      Sheet1.Visible = xlSheetHidden
      Sheet1.Visible = False 'Same thing
  
      Debug.Print Sheet1.Visible 'Prints 0 (xlSheetHidden)
  
      Sheet1.Visible = xlSheetVisible
      Sheet1.Visible = True 'Same thing
  
      Debug.Print Sheet1.Visible 'Prints -1 (xlSheetVisible)
  
      ThisWorkbook.Save
  
  End Sub
  
  Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
  
  
      Debug.Print Sheet1.Visible 'Prints -1 (xlSheetVisible)
  
      Sheet1.Visible = xlSheetHidden
      Sheet1.Visible = False 'Same thing
  
      'Sheet1 should now be hidden, but it's not
      Debug.Print Sheet1.Visible 'Prints -1 (xlSheetVisible)
  
  End Sub
  </code></pre>
  
  <p>Hiding and unhiding sheets works perfectly in the first event, but as soon as the second event is triggered, the sheet visibility does not change.</p>
  
  <p>Update 3:</p>
  
  <p>Another point. If I enter the Workbook_BeforeSave() event directly (by saving), rather than entering it from the Workbook_BeforeClose() event, then everything works as expected.</p>
  
  <p>MUCH LATER UPDATE:</p>
  
  <p>Although I marked this question as solved a while ago, I've come across an article that helps to explain the behaviour. An explanation can be found at:
  <a href="http://www.cpearson.com/excel/events.aspx" rel="nofollow noreferrer">http://www.cpearson.com/excel/events.aspx</a></p>