Escape from UserForm

L

Lasse

Hi

I have created a UserForm and a macro to open it with. How can I get it to
close the UserForm if I press Escape?
I have a OK and Cancel button and both works fine, but would like to
implement the Escape button as well.

/Lasse
 
J

Jay Freedman

Lasse said:
Hi

I have created a UserForm and a macro to open it with. How can I get
it to close the UserForm if I press Escape?
I have a OK and Cancel button and both works fine, but would like to
implement the Escape button as well.

/Lasse

In the userform designer window, display the Properties pane (F4). Select
the Cancel button on the form and set its Cancel property to True.

Quoting the VBA help topic for the Cancel property,

"When a CommandButton's Cancel property is set to True and the form is the
active form, the user can choose the command button by clicking it, pressing
ESC, or pressing ENTER when the button has the focus."

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
L

Lasse

Hi Jay

Thansk for your reply.

I have just tried to do as you wrote but I am having some problems. I have
created a Userform with a Multipage with 4 pages. On each page I have created
a Cancel button and tried changing the Cancel property from False to True,
but it doesn't seem to remember the change. I can get it to remember the
change on one page but then it doesn't work on the 3 other pages.
I have created a macro which calls the Userform, is it possible to add some
code that enables ESCAPE instead of changing it through the properties?

/Lasse
 
J

Jay Freedman

You're going about it wrong. The OK and Cancel buttons should be on the
underlying userform surface, not on the individual pages of the multipage
control.

If you rename the Cancel button, let's say it's named btnCancel, then you
can set the property in code (typically in the Userform_Initialize
procedure) with the line

btnCancel.Cancel = True

instead of (or in addition to) setting it in the Properties dialog. This
method is available for any property of any control on the userform.
 

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