Before closing workbook event

TheWennerWoman

Active Member
Joined
Aug 1, 2019
Messages
270
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have some code that is triggered when the user clicks the red x to close the workbook. The code opens a form and the user has two options "yes" and "no".

If they click no I just want the form to disappear and they were as they were before it popped up. I tried
VBA Code:
Private Sub CommandButton2_Click()
    Cancel = True
    Me.Hide
   
End Sub

but the workbook still closes.

How do I force it to just close the userform down? It's remembering the fact that the red x was clicked.

Many thanks.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi,
to prevent the UserForm being closed by pressing red X add this code to your form

VBA Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
   Cancel = CloseMode = 0
End Sub

Do ensure that you have another method to close the form

example:
VBA Code:
Private Sub CommandButton1_Click()
 Unload Me
End Sub

Dave
 
Upvote 0
Hi, thanks for that but it's not the userform being closed that's my problem.

User tries to close the workbook - a userform pops up giving them a "have you reviewed this data and can vouch for it's accuracy?" with three buttons "Yes", "No", "Finance Use Only".

If they click No, I want the userform to disappear and the workbook to remain open. As it stands, they click No, the form disappears but the workbook still closes.
 
Upvote 0
I suspect you would need to use the Workbook_BeforeClose event in order to have the Cancel = True to prevent the closing of the workbook.
 
Upvote 0
Hi,
my misunderstanding - You would use the Workbook_BeforeClose event to Cancel the close operation

Dave
 
Upvote 0
Many thanks both. This is messier than it should be :) So putting cancel = true in the Workbook_BeforeClose event does the trick if the user clicks No.......except, if the user clicks Yes or Finance Use Only, I'd want the workbook to close! Can I force the workbook to close in the CommandButton1 or CommandButton3 bits of code?
 
Upvote 0
The Cancel = True in the Workbook_BeforeClose event has nothing to do with your user form.
Call the user form from Workbook_BeforeClose event.
When the user form closes code execution will return to Workbook_BeforeClose.

Put a break point at the start of the Workbook_BeforeClose and continue things with the F8 key to see where/how the code operates.
 
Upvote 0
Many thanks both. This is messier than it should be :) So putting cancel = true in the Workbook_BeforeClose event does the trick if the user clicks No.......except, if the user clicks Yes or Finance Use Only, I'd want the workbook to close! Can I force the workbook to close in the CommandButton1 or CommandButton3 bits of code?

You can return a value form your userform to do what you want.
Helpful if publish the forms code

Dave
 
Upvote 0
The Cancel = True in the Workbook_BeforeClose event has nothing to do with your user form.
Call the user form from Workbook_BeforeClose event.
When the user form closes code execution will return to Workbook_BeforeClose.

Put a break point at the start of the Workbook_BeforeClose and continue things with the F8 key to see where/how the code operates.

So Cancel = True in the Workbook_BeforeClose event doesn't cancel the workbook close?
VBA Code:
Private Sub Workbook_BeforeClose(Cancel as Boolean)
UserForm10.Show
End Sub

Code in UserForm10 is typical for the three buttons
VBA Code:
Private Sub CommandButton1_Click()
'do something here, user has clicked Yes
Me.Hide
ActiveWorkbook.Close SaveChanges:=True
End Sub

Private Sub CommandButton2_Click()
'don't do anything here, user has clicked No
Me.Hide
End Sub

Private Sub CommandButton3_Click()
'do something here, user has clicked Finance Use Only
Me.Hide
ActiveWorkbook.Close SaveChanges:=True
End Sub

Clicking button 1 or 3 keeps the workbook alive when I don't want it to? Clicking button 2 keeps it live (which is good!)
 
Upvote 0
Hi,
try following & see if helps

Code for your UserForm


VBA Code:
Private Sub CommandButton1_Click()
'Yes Button
'do something here, user has clicked Yes
    Me.Hide
    Me.Tag = False
End Sub

Private Sub CommandButton2_Click()
'No Button
'don't do anything here, user has clicked No
    Me.Tag = True
    Me.Hide
End Sub

Private Sub CommandButton3_Click()
'Finance Button
'do something here, user has clicked Finance Use Only
Me.Hide
Me.Tag = False
End Sub

Function GetResponse() As Boolean
    Me.Show
    GetResponse = CBool(Me.Tag)
    Unload Me
End Function

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = CloseMode = 0
    'if user clicks red X call No Button
    If Cancel Then Call CommandButton2_Click
End Sub

Code for Workbook Close event

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Cancel = UserForm10.GetResponse
End Sub

As an alternative you could use the MsgBox function that has Yes No Cancel buttons

Hope helpful

Dave
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,058
Members
449,206
Latest member
Healthydogs

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