title: VBA: Workbook.Close loop
body: <p>I have the following portion of the code for a given active worksheet:</p>
  
  <pre><code>Private Sub Workbook_BeforeClose(Cancel As Boolean)
  
  Dim sh As Worksheet
  Dim myMessage
      If ActiveSheet.Range("I5").Value &gt; 0 Then
          MsgBox "Some cells are empty" _
          &amp; Chr(13) &amp; Chr(13) &amp; "Please, fill in the empty cells to proceed", _
          vbExclamation + vbOKOnly, "ERROR!"
          Cancel = True
      Else
          ThisWorkbook.Unprotect ("123")
          Worksheets("Instructions").Visible = True
          Worksheets("Instructions").Activate
          For Each sh In Worksheets
              If sh.Name &lt;&gt; "Instructions" Then
                  sh.Visible = xlVeryHidden
              End If
          Next sh
          ThisWorkbook.Protect ("123")
          myMessage = MsgBox("Do you want to save and close this file?", vbQuestion + vbYesNo, "Workbook 1")
              If myMessage = vbYes Then
                  Application.DisplayFormulaBar = True                    
                  ThisWorkbook.Close True
              ElseIf myMessage = vbNo Then
                  Cancel = True
              Else
                  'nothing
              End If
  
      End If
  End Sub
  </code></pre>
  
  <p>In general, upon pressing the close button on the workbook itself, the idea is to check a given cell of a given active worksheet for a non-zero value. If it is non-zero, the user has to fill in certain empty cells first and the workbook is not closed. Alternatively, the "Instruction" worksheet is activated and the user receives a message box of what to do next (by doing this, instead of simply no msgbox and sticking with the usual system "SAVE/NO" prompt, is that I want to eliminate the possibility of a user pressing the "No" button, which will lead to no save being made). The only problem I have is the one-time loop I get when a user selects "Yes" answer: the same message box re-appears (apparently, because the procedure goes through the code line again!). 
  I would appreciate if you could suggest me the way I could avoid this loop.</p>