VBA Workbook_BeforeClose doesn't fire twice in a row

ramulose

New Member
Joined
Oct 27, 2015
Messages
33
This is an interesting problem that can be reproduced with only 4 lines of code so I think it might be a bug, unless I am doing something wrong (Which is entirely possible).

To recreate the problem, execute the following steps.

1. Open a new workbook
2. Copy the following code into the ThisWorkbook object

Private Sub Workbook_BeforeClose(Cancel As Boolean)
UserResponse = MsgBox("You wanna to bail?", vbYesNo)
If UserResponse = vbNo Then
Cancel = True
End If
End Sub

3. Save the file
4. Click the X in the upper right corner of the SPREADSHEET to close it
5. It will ask "Wanna Bail?"
6. Answer 'No'
7. (It doesn't close as expected)
7. Click the X in the upper right corner of the SPREADSHEET again.
8. The spreadsheet closes without firing the event

Any insights?

Thanks!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Code works fine for me on both the first and second attempts to close.
 
Upvote 0
dude.. I have just checked and it's working fine.
might be you need to take one more try.

Code:
 Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
 UserResponse = MsgBox("You wanna to bail?", vbYesNo)
 
 If UserResponse = vbNo Then
    Cancel = True
 End If
 
 End Sub
 
Upvote 0
Thanks guys. I have tried many, many times (Maybe 20 or 30?). It's definitely not working here. Do either of you use 2016? I'm wondering if it could have anything to do with that.
 
Upvote 0
By the way. How do you use code tags so the code looks like code? I can't find them in the editor.
 
Upvote 0
first press alt+f11

click to thisworkbook and paste entire code then save.
then try to close and select NO on msgbox
 
Upvote 0
WOW!!!! I figured this out. If I add am Application.EnableEvents call after the 'No' Response, it works! Very strange.

Here's the code:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
UserResponse = MsgBox("You wanna bail?", vbYesNo)
If UserResponse = vbNo Then
Cancel = True
Application.EnableEvents = True
End If
End Sub

It seems like events are being turned off when you cancel. This could be an issue with 2016. If you know anyone that has it, they should try it out.

The fix is simple, but the reason for it is a little disconcerting. This could indicate that other things are broken with this new version as well.

If I can still edit my original post, I will add 2016 to the tags and the title.

Thanks for the responses guys.
 
Upvote 0
ok .. can you tell me what is the file extension and in name of the module where you have the code .
 
Upvote 0
So it doesn't look like I can edit my OP. If you discover that this is a problem with 2016, one of the administrators might want to add the '2016' tag to it.
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,578
Members
449,174
Latest member
chandan4057

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