Forcing a userform to close

quackdad

Board Regular
Joined
Jul 23, 2013
Messages
110
I am using "UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)" to close a userform because I need to trigger the exact same action whether some one clicks on the Cancel button I've created, or the little red X.
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = False
    If Range("C2") = "Endrun" Then
        Application.Run "Rearrange"
    Else:
        Application.Run "StatusCancel"
    End If
End Sub

Private Sub EmpStatusCancel_Click()
    Unload Me
End Sub

Private Sub EmpStatusDone_Click()
    Range("C2") = "Endrun"
    Unload Me
End Sub
If someone clicks on the EmpStatusDone button, then the macro "Rearrange" is run. The first thing this macro does is switch to a different sheet. At this point I need to have the userform close, because the macro will launch a new userform, and I don't want to have both on the screen at the same time. Do I need to put things in a different order (not that there's a lot of choice here), or is there something I can add to the "Rearrange" macro?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
If you want the same thing to happen however the user closed the userform then you shouldn't need any code, apart from Unload Me, in the buttons to close the userform.

Unload Me will trigger the QueryClose event.
 
Upvote 0
The code in the EmpStatusDone event determines which macro is run when the userform is closed. My problem was getting the userform to close before running the macro "Rearrange", which brings up another userform, and that userform had to be clsoed before this one would. But I finally figured out that the key is in calling the macro after the close event. I just had to add a trigger to make sure the other macro runs on cancel.
Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Cancel = False
    If Range("F6") = "Exit" Then
        Application.Run "StatusCancel"
    End If
End Sub
 
Private Sub EmpStatusCancel_Click()
    Range("F6") = "Exit"
    Unload Me
End Sub
 
Private Sub EmpStatusDone_Click()
    Unload Me
    Application.Run "Rearrange"
End Sub

Thank you for the response. I apologize for having posted this, when I was fully capable of finding an answer. But my brain was fried after a couple of hours of working on this. Took a 3 hour break, and eventually it fell together for me. (There's a lesson in there somewhere).
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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