Closing Excel Userform "X"

sassriverrat

Well-known Member
Joined
Oct 4, 2018
Messages
655
Good Morning,

I wrote this piece of code to attempt to close excel if the userform "X" is used to close the userform. Basically the userform opens when excel does and asks for one of two buttons. If neither button is pushed (i.e. someone closes the userform), then I want excel to close.

please note that clicking either of the buttons in the userform unloads it so I tried an Userform_Terminate() and had a terrible time getting the workbook back open to delete that...

Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)MsgBox "Do you really want to close?", vbYesNoCancel
    If vbYes Then
        'Application Closer
        If Workbooks.Count > 1 Then
            ActiveWorkbook.Close
        Else: Application.Quit
        End If
    Else: Exit Sub
    End If
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You could try

Code:
[COLOR=#333333]Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)[/COLOR]
[COLOR=#333333]If CloseMode = 0 Then Cancel = True[/COLOR]
[COLOR=#333333]End Sub
[/COLOR]
 
Upvote 0
Well its designed to stop the close. Replace the "Cancel = True" with what you want to happen.
 
Upvote 0
Perfect.

Here's the tweaked in case anyone needs/wants it!

Code:
  Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)If CloseMode = 0 Then
    'Application Closer
    If Workbooks.Count > 1 Then
        ActiveWorkbook.Close
    Else: Application.Quit
    End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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