Msg Box does not close after clicking YES

CA_User

Board Regular
Joined
Nov 5, 2004
Messages
124
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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
24,208
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
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
37,884
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,290
Messages
5,836,445
Members
430,430
Latest member
Tomexcel2022

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
Top