enter key in a user form macro

patupatu

New Member
Joined
Apr 7, 2011
Messages
23
I want my userform to do "somthing" when the user hits the enter key. for example, the user enters a code and when he hits enter my program (privet sub) goes and finds a record in agiven worksheet and then show the content in the user form. A typical inquiry. NOT via a command button
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Maybe like this in the userform's code module

Code:
Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 13 Then MsgBox "Enter pressed"
End Sub
 
Upvote 0
Thank you very much. The thing is that I don't know where to put it. Below is a simplify code of my userform Where should I insert you code and how does it work? I thought it will br like an event something like "on enter eky DO..."

I'd appreciate your time and help.

CU

Private Sub CmdConsultar_Click()
' ultimafila = Cells(Rows.Count, 1).End(xlUp).Row


If TxtCodigo = "" Then
MsgBox ("empty code")
ConsultadeServicios.Hide
Exit Sub
Else
TxtDescripcion = Application.WorksheetFunction. _
VLookup(TxtCodigo.Text, Worksheets("Sheet1").Range("a2:b20"), 2, False)
If TxtDescripcion = "" Then
MsgBox ("Code does not exists")
End If
End If



End Sub



Maybe like this in the userform's code module

Code:
Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 13 Then MsgBox "Enter pressed"
End Sub
 
Upvote 0
If you put the code in the userform's code module, it will run whenever Enter is pressed

Code:
Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 13 Then
    '
    'your code here
    '
End If
End Sub
 
Upvote 0
Thank you. However, for every key I stroke my SUB is called, except when I hit "Intro". ???

I hope you can help me Thank you

Here is my code:
_______________________________________________

Private Sub TxtCodigo_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If KeyAscii = 13 Then
Call CmdConsultar_Click

End If

End Sub
_____________________________________________________
Private Sub CmdConsultar_Click()
' ultimafila = Cells(Rows.Count, 1).End(xlUp).Row

If TxtCodigo = "" Then
MsgBox ("empty code")
ConsultadeServicios.Hide
Exit Sub

Else

TxtDescripcion = Application.WorksheetFunction. _
VLookup(TxtCodigo.Text, Worksheets("Sheet1").Range("a2:b20"), 2, False)
If TxtDescripcion = "" Then
MsgBox ("Code does not exists")
End If
End If

End Sub
___________________________________________________

If you put the code in the userform's code module, it will run whenever Enter is pressed

Code:
Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 13 Then
    '
    'your code here
    '
End If
End Sub
 
Upvote 0
Hello again. I just checked and ..._keypress recognizes ANSI characters only or what it's called "typeables", therefore "intro" key doesn't invoke txtfield_KeyPress

Any ideas?

Thank you


Thank you. However, for every key I stroke my SUB is called, except when I hit "Intro". ???

I hope you can help me Thank you

Here is my code:
_______________________________________________

Private Sub TxtCodigo_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

If KeyAscii = 13 Then
Call CmdConsultar_Click

End If

End Sub
_____________________________________________________
Private Sub CmdConsultar_Click()
' ultimafila = Cells(Rows.Count, 1).End(xlUp).Row

If TxtCodigo = "" Then
MsgBox ("empty code")
ConsultadeServicios.Hide
Exit Sub

Else

TxtDescripcion = Application.WorksheetFunction. _
VLookup(TxtCodigo.Text, Worksheets("Sheet1").Range("a2:b20"), 2, False)
If TxtDescripcion = "" Then
MsgBox ("Code does not exists")
End If
End If

End Sub
___________________________________________________
 
Upvote 0
Try this :

Code:
Private Sub TxtCodigo_KeyDown _
(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

    If KeyCode = 13 Then Call CmdConsultar_Click

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,308
Members
452,904
Latest member
CodeMasterX

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