How to use the 'X' button to cancel

SamsonL

New Member
Joined
Jan 30, 2017
Messages
7
Hello Everyone,

I am having trouble when using my forms. Whenever I used this code, the form will not allow me to close the form using the 'X' button and instead having to click cancel to close. I have been tasked to takeover a marco from a previous designer and I am wondering if there is a way to get around using the X instead of cancel.



Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "Use the Cancel Button!"
Cancel = True
End If
End Sub

Thanks!
Private Sub Cancel_Click()
Tag = "Closed"
Me.Hide
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
It looks like the previous designer didn't want the userform to Unload, but only to hide.
Why is that? (Do you want to keep that)

I can see two ways to go about this.
1) Delete the whole QueryClose event. That will allow the X to unload the userform. (Hence my question about why only hide.)

2) Change the code to this. The corner x will hide the userform. But code that unloads the form will exicute. The only way to unload the UF will be via code from somewhere.

Code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    Call Cancel_Click
    Cancel = (CloseMode = vbFormControlMenu)
End Sub
 
Upvote 0
That has worked perfectly thank you. I am not to sure why the previous designer wanted to hide these UF's. Something that I will have to look into for sure. Appreciate it
 
Upvote 0
Does the calling code refer to any elements of the form after it is hidden? Hopefully properties, but maybe control contents (text in a textbox for example)?

What bothers me is people who bypass that red X but still tell the user to click a different button to close. The user clicked the red X, man, just close the form already.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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