Msg Box does not close after clicking YES

CA_User

Board Regular
Joined
Nov 5, 2004
Messages
126
I have the following code in ThisWorkbook

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("Did you PW protect this file?", vbInformation + vbYesNo + vbDefaultButton1, "Please Confirm before Closing") = vbNo Then
Cancel = True
Else
ActiveWorkbook.Close
End If
End Sub


When I click yes, the pop up message does not close. I have to click it once more and then it will exit. How can I make it close after clicking "yes" just once?

thank you.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
You have walked yourself into an infinite loop.

"YES" leads to AcitveWorkbook.Close, which invokes the Before_Close event yet again.


Just omit the line
ActiveWorkbook.Close
 
Upvote 0
I have the following code in ThisWorkbook

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("Did you PW protect this file?", vbInformation + vbYesNo + vbDefaultButton1, "Please Confirm before Closing") = vbNo Then
Cancel = True
Else
ActiveWorkbook.Close
End If
End Sub

When I click yes, the pop up message does not close. I have to click it once more and then it will exit. How can I make it close after clicking "yes" just once?
You do not need the ActiveWorkbook.Close statement... the whole reason the code is executing is because the workbook is in the process of being closed. I think that line makes it close again which kicks off your code again. Remove that line and your code should work as expected. By the way, you can reduce your code to this one-liner...

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
  Cancel = (MsgBox("Did you PW protect this file?", vbInformation + vbYesNo + _
           vbDefaultButton1, "Please Confirm before Closing") = vbNo)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,591
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top