Share via

Force an enter key code in VBA?

Anonymous
2013-10-27T19:54:11+00:00

Hi!

I have a textbox on a userform and I want to know if there's any way to force or write an enter key code via VBA, so that when I enter data, Excel hits enter automatically each time without having to click on CommandButton or hit enter key.

Thanks!!

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2013-10-27T20:04:25+00:00

Private Sub TextBox1_Change()

  If Len(TextBox1) = 4 Then

    'simulate hitting the [Enter] key

    'by having it move to the next

    'control in sequence on the form

    'or you could move to another control

    Me.TextBox2.SetFocus

    'or move to button and click it!

    CommandButton1.SetFocus

    CommandButton1_Click

  End If

End Sub

Private Sub CommandButton1_Click()

  MsgBox "You entered 4 characters"

End Sub

The _Change() event fires each time you type into the text box, so once you've got the 4th character (in my example) in it, it goes off and does other stuff.  You could validate the entry at this point also.  Thing to remember here is you've kind of got to get that last character right!  You can edit and backspace and such until you do get 4 characters (or other number you choose), but as soon as that last one goes in, then the code will take over.

BTW: Didn't have to move to the CommandButton before calling its _Click() event, but it was a way to get out of the TextBox1 control itself.

Was this answer helpful?

0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2013-10-28T23:00:46+00:00

    Thank you for your feedback, and you're certainly most welcome; glad to hear it is helping you.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2013-10-28T16:59:25+00:00

    Thank you so much! This worked whe I enter code manually on the textbox.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2013-10-27T23:38:41+00:00

    I don't know - but at least the code is free, so try it and see?  The code is designed for use with a UserForm - not sure just where your barcode reader puts the input, a lot would depend on that.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2013-10-27T22:49:59+00:00

    Oh! Thank you. I enter data through a barcode reader. Will this code work as well?

    Thanks!!

    Was this answer helpful?

    0 comments No comments