Enter button keydown advice

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,
I have a userform where i type in txtInput field a value then pressing the btkOK button runs the code.
I just dont see what triggers the code to close the userform & advise me the answer as the code for the btnOK button is just Hide ??

Basically im trying to just run the code with pressing Enter on the keyboard

Rich (BB code):
Private Sub CommandButton1_Click()
   
    Dim ans As String
    Dim Fnd As Range
    Dim varNRows As Long
    Dim varFndRow As Long

    varNRows = Cells(Rows.Count, "A").End(xlUp).Row
    Range("A4:" & "A" & varNRows).Interior.Color = vbGreen
    HondaListVinForm.Show
    ans = HondaListVinForm.txtInput.Text
    HondaListVinForm.txtInput.Text = ""
    HondaListVinForm.txtInput.SetFocus
    If ans = "" Then Exit Sub
EX:
    Set Fnd = Range("A4:A" & varNRows).Find _
        (ans, Range("A" & varNRows), , xlPart, , , False, , False)
    If Not Fnd Is Nothing Then
        varFndRow = Fnd.Row
        Cells(varFndRow, "A").Interior.Color = RGB(51, 255, 255)
        MsgBox "THE NEAREST MATCH IS AT ROW " & varFndRow, vbInformation
        Cells(varFndRow, "A").Select
    Else
        ans = Left(ans, Len(ans) - 1)
        GoTo EX
    End If
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Do you mean you want to close the userform when the Enter key is pressed or are you trying to find out why the userform closes when you press Enter?
 
Upvote 0
Morning,
I type the value in the userform and at this time userform is still open.
Its not until i click the OK button that the userform closes & asvises me the row number in question.

1,I cant see how the OK button does anything than hide userform.
2,I am trying to get away from pressing ok each time just hitting Enter for me is better / easier
 
Upvote 0
Still not 100% sure what you want, particularly since there's nothing that hides/unloads the userform in the posted code.

Anyway, if you want the code of a button to run when you press Enter set the Default property to True.
 
Upvote 0
This is all the code
I didnt put the other code here as i mentioned for the ok button it just shows HIDE




Rich (BB code):
Private Sub CommandButton1_Click()
   
    Dim ans As String
    Dim Fnd As Range
    Dim varNRows As Long
    Dim varFndRow As Long

    varNRows = Cells(Rows.Count, "A").End(xlUp).Row
    Range("A4:" & "A" & varNRows).Interior.Color = vbGreen
    HondaListVinForm.Show
    ans = HondaListVinForm.txtInput.Text
    HondaListVinForm.txtInput.Text = ""
    HondaListVinForm.txtInput.SetFocus
    If ans = "" Then Exit Sub
EX:
    Set Fnd = Range("A4:A" & varNRows).Find _
        (ans, Range("A" & varNRows), , xlPart, , , False, , False)
    If Not Fnd Is Nothing Then
        varFndRow = Fnd.Row
        Cells(varFndRow, "A").Interior.Color = RGB(51, 255, 255)
        MsgBox "THE NEAREST MATCH IS AT ROW " & varFndRow, vbInformation
        Cells(varFndRow, "A").Select
    Else
        ans = Left(ans, Len(ans) - 1)
        GoTo EX
    End If
    
    End Sub

Rich (BB code):
Private Sub UserForm_Initialize()
   
    txtInput.TextAlign = fmTextAlignCenter
    txtInput.SetFocus
    txtInput.Text = ""
    lblInputBox.Caption = "ENTER A PARTIAL VIN TO SEARCH FOR"
    Caption = "Honda Vin Tool"

   
    Me.StartUpPosition = 0
    Me.Top = Application.Top + 55  ' MARGIN FROM TOP OF SCREEN
    Me.Left = Application.Left + Application.Width - Me.Width - 370 ' LEFT / RIGHT OF SCREEN
   
   
End Sub

Private Sub txtInput_Change()
   
    txtInput.Text = UCase(txtInput.Text)
   
End Sub

Private Sub btnCancel_Click()
   
    txtInput.Text = ""
    Hide
    Range("A4").Select
End Sub

Private Sub btnOK_Click()
   
    Hide
   
End Sub
 
Upvote 0
I'm sorry but I'm still not sure what you want to do.

If you want to run the code for any button when you click Enter you set it's Default property to True.
 
Upvote 0
Im also confused by the answer.

Please show me in the code what triggers the search because the ok button just tells the form to close.
 
Upvote 0
@Norie
I wish to type the value in the textbox
At this point i wish to press ENTER on my keyboard to let the code find my value in column A


My problem is that i do not know what to write correctly or where to put it so the code runs.
 
Upvote 0
Still having an issue with this.
After i type my value in the textbox1 field i hit ENTER which only then sets focus on the OK button.
If i press ENTER again the form closes & the code runs.

I would like NOT to press ENTER twice,why would i need to do that ?
Hence why i am trying to add code so when i hit ENTER once then the code should run.

The current code forces the user to hit ENTER twice or just to click the OK button.
 
Upvote 0
This is what was needed.

Rich (BB code):
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyReturn Then
    Hide
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,560
Messages
6,120,217
Members
448,951
Latest member
jennlynn

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