Enter button keydown advice

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
4,079
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

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
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?
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
4,079
Office Version
  1. 2007
Platform
  1. Windows
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
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.
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
4,079
Office Version
  1. 2007
Platform
  1. Windows

ADVERTISEMENT

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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
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.
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
4,079
Office Version
  1. 2007
Platform
  1. Windows

ADVERTISEMENT

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.
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
4,079
Office Version
  1. 2007
Platform
  1. Windows
@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.
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
4,079
Office Version
  1. 2007
Platform
  1. Windows
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.
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
4,079
Office Version
  1. 2007
Platform
  1. Windows
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
 

Forum statistics

Threads
1,141,613
Messages
5,707,394
Members
421,508
Latest member
Jalayne

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
Top