Don't use command button in a userform

still learning

Well-known Member
Joined
Jan 15, 2010
Messages
784
Office Version
  1. 365
Platform
  1. Windows
Hi

How do I change the following userform code to have the user press enter after a name is entered, instead of having a command botton?
VBA Code:
Private Sub TextBox1_Change()
End Sub

Private Sub TextBox5_Change()
End Sub

Private Sub CommandButton1_Click()
With TextBox5      'last name
ActiveSheet.Range("p2").Value = .Text
End With
With TextBox1      'first name
ActiveSheet.Range("q2").Value = .Text
End With
Me.Hide
Findd       ‘ this is the macro that will find the name using “advance filter”
Unload Me
End Sub

mike
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hello stilllearning,
use KeyDown event for each Textbox.
For example...
VBA Code:
Private Sub TextBox5_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    
     If KeyCode = 13 Then
        With ActiveSheet
            .Range("p2").Value = TextBox5.Text
            .Range("q2").Value = TextBox1.Text
        End With
        Me.Hide
        Findd
        Unload Me
     End If
     
End Sub
 
Last edited:
Upvote 0
Each key on keyboard have number.
13 is the number of the "Enter" key.
You can check key number with
inserting in the textbox KeyDown event "Msgbox Keycode".
For example...
VBA Code:
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
   
    MsgBox KeyCode
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,252
Members
449,075
Latest member
staticfluids

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