Close UserForm by pressing Esc key


S

Shazi

Hi,

I made a userform and I want to close this without pressing the Cancel
button or Close Button. is it possible to close it by pressing Esc
key.

Pls advise me.

Regards.

Shahzad
 
Ad

Advertisements

R

RyanH

I think the Userform will close with the Esc Key pressed by default. It does
for me in Excel 2007.

If not, I'd use,

Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)

If KeyCode = 27 Then
Unload Me
End If

End Sub
 
P

Peter T

Your form needs a button that has it's Cancel property = true. Put your code
to close the form in the cancel button's click event, eg

Private Sub CommandButton1_Click()
Unload Me
End Sub

Typically the button's caption would read "Cancel" or "Quit", so you could
click it or press Esc, but the button could be hidden off the form if you
want.

Regards,
Peter T
 
Ad

Advertisements

R

RyanH

lol, I've been using Peter T method without even realizing it. Peters
response is the way to go, it works for me. The Esc Key will not close the
UserForm by default.

Look under the Properties Window of the command button you wish to use to
Unload the Userform. Set the Cancel Property = True. Sorry for any
confusion.

Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top