How to close Userform using Escape

LeanidTerbant

New Member
Joined
Nov 15, 2010
Messages
16
Hi guys,

I was wondering if there is a way to set things so that my userform will shut upon pressing the Escape key.
I am aware that were I to have a button on my userform which closed the userform, I could just set Cancel to True within its properties, but I don't have such a button, and even if I did, my userform is a multipage one with no space to put a button such that it would be visible from every page.

What I am looking for here is simply a way to avoid having to click the cross in the top corner every time I want to exit the form.

Thanks
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Place a Textbox on the form and set its Height and Width to 0. Arrange your form so that it has focus whenever you want the Escape key to be enabled and place this in your user form's code module:-
Code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If KeyAscii = 27 Then Unload Me
End Sub
Tested & working here.
 
Upvote 0
That is indeed pretty clever, thanks. :)

The only thing is that I want the Escape key to be enabled at basically any time, so presumably I'm going to have to write this for every single textbox/combobox/optionbutton/checkbox in the form (and there are about 100 altogether) :(
 
Upvote 0
Same as Ruddles, but on the form:

Code:
Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 27 Then Unload Me
End Sub

Edit: Forget that, Keydown doesn't fire if a control has the focus. :(
 
Upvote 0
if all the objects where members of the same class, could you have a simple class module which tests the keydown event?
 
Upvote 0
Why not just add a button? You can stick it behind the multipage if you want, though i personally think it's better for the user to have an obvious cancel/close button (they shouldn't be using the X really, IMO)
 
Upvote 0
Thanks Rory
Sticking a button behind the multipage worked...I should have thought of that really. As for making an obvious close/cancel button, I designed the form a while ago, and at the time it was not supposed to be able to be closed at all. Because of this, the layout is kinda established (the users are already familiar with it), it's just that now, there are extra sheets, not directly related to the userform, that need to be able to be viewed. In other words, you're probably right about having a proper "close" button, but it would be not worth the hassle of changing the form's layout.
 
Upvote 0
Same as Ruddles, but on the form:

Code:
Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 27 Then Unload Me
End Sub

Edit: Forget that, Keydown doesn't fire if a control has the focus. :(

Oh my gosh. I'm so glad you decided to post this despite your note at the end, because it solved a problem I had. Of course every solution just leads to the next problem...

What if I actually want to return the key that the user entered that closed the form?

The best I could come up with is this:

Code:
Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If Shift = 0 Then Selection.TypeText LCase(Chr(KeyCode))
    If Shift = 1 Then Selection.TypeText Chr(KeyCode)
    Unload Me
End Sub

But it still doesn't return the right characters for anything other than a number or letter. It's rare, but if the user is trying to type a ( or ) or other non-alphanumeric character, this gives an incorrect result. Any ideas? (And why does it always return the code for the uppercase letter, regardless of what's typed? That's weird, no?)
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,931
Members
449,480
Latest member
yesitisasport

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