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
 
Edit: Forget that, Keydown doesn't fire if a control has the focus. :(

The solution I hit upon was to give every control on the form a Keypress event that passes the keypress to a generic macro for processing. So, for each control, create something like this:

Rich (BB code):
Private Sub TextBox1_Keypress(ByVal KeyAscii As MSForms.ReturnInteger)
Call FormGenericKeys(KeyAscii)​
End Sub

And this is the macro that actually processes the keypresses:

Rich (BB code):
Sub FormGenericKeys(ByVal KeyAscii As MSForms.ReturnInteger)
'If ESC is pressed, close the UserForm
If KeyAscii = 27 Then Unload Me​
End Sub

It's not as elegant as being able to set Keypress events on the form itself, but it does allow you to add or change the behaviour in one place.

In the event that one of your controls has its own keypress events, just put them in an If statement first, and then send any keypresses that don't match that to the macro:

Rich (BB code):
Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'If the user hits ENTER, execute some specific code
If KeyAscii = 13 Then​
'Your code for what happens when the user hits ENTER when this control is selected goes here
'If the user hits any other key, it sends it to the FormGenericKeys macro for processing
Else:​
Call FormGenericKeys(KeyAscii)​
End If​
End Sub
 
Last edited:
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Not sure if I'm kicking in a open door here. But I solved it by adding a command button, set the property 'Cancel' to 'TRUE' and the code
VBA Code:
Private Sub Button_Close_Click()
 Unload Me
End Sub
And the then hide the button behind a text box.
 
Upvote 0
The OP (in this 11 year old thread ;)) stated he knew he could do that but didn’t want to…
 
Upvote 0

Forum statistics

Threads
1,221,575
Messages
6,160,603
Members
451,657
Latest member
Ang24

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