Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

detect enter key

Status
Not open for further replies.

maswien

Technical User
Sep 24, 2003
1,286
CA

How to detect the enter key is pressed in a user form of Excel?
 
Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 13 Then
'User pressed the enter key...do something
Else
'User pressed some other key
End If
End Sub

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
cLFlaVA, the Form KeyPress event procedure is triggered only if this form has no control or all the controls are disable.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi mjia,

Would you mind if I ask WHY you want to detect when Enter is pressed? Various actions happen when a User presses the Enter key; most, if not all, of the actions can be achieved in some other way so detecting the key press is not a very useful way of detecting User intention.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
PHV-

So then mjia would potentially have to capture the KeyPress event on each and any control that exists on the form.

ie, any textboxes, control buttons, etc.

*cLFlaVA
----------------------------
A polar bear walks into a bar and says, "Can I have a ... beer?"
The bartender asks, "What's with the big pause?
 
Provided NO button is the Enter one ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 

The user form has a textbox and a button, so I expect that pressing the button or pressing enter both do the submit.

 
Hi mjia,

If you set the button property Default to True, then pressing Enter will act exactly as though the button were pressed.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top