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

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,985
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
36,980
Office Version
  1. 2016
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
 

Watch MrExcel Video

Forum statistics

Threads
1,130,112
Messages
5,640,173
Members
417,129
Latest member
geekzilla

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