UserForm textBox Run Sub on Enter Press

jjlafond

Board Regular
Joined
Jul 17, 2014
Messages
56
Hello,

I have a UserForm with lots of textBoxes. At the bottom of the form is a clickable "Enter Data" button. I would like to be able to run the "Enter Data" sub by pressing the enter key on the keyboard in any of the textboxes.

I think it would work (??) by creating a sub for each textbox containing:
Code:
On Enter button
     Call Enter_Data

But I'm not at all sure how to set up code for this. Any help would be greatly appreciated.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Something like this:

Private Sub CommandButton1_Click()
'do your stuff here - this is the enter button
End Sub
Private Sub TextBox1_AfterUpdate()
CommandButton1_Click
End Sub
Private Sub TextBox2_AfterUpdate()
CommandButton1_Click
End Sub
Private Sub TextBox3_AfterUpdate()
CommandButton1_Click
End Sub
Private Sub TextBox4_AfterUpdate()
CommandButton1_Click
End Sub
 
Upvote 0
BobUmlas said:
Something like this:
Code:
[COLOR=#333333]Private Sub CommandButton1_Click()[/COLOR]
[COLOR=#333333]'do your stuff here - this is the enter button[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
[COLOR=#333333]Private Sub TextBox1_AfterUpdate()[/COLOR]
[COLOR=#333333]CommandButton1_Click[/COLOR]
[COLOR=#333333]End Sub[/COLOR]

This was a great suggestion, and it worked well, it wasn't quite what I was looking for. The above code went to CommandButton_1_Click well, but I needed something that worked on a keyboard key so that I could also make it work on OptionButtons as well.

I finally found one other post and modified it to what I was looking for. This takes the _AfterUpdates idea and turns it into _OnEnterPush.
The following method also works on option buttons. I can now press Enter to select the button, press space to select the button, and press enter to advance on.

Code:
Private Sub Frac1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then

If conditions Then
  Do something
Else: Call NextSub
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,216,759
Messages
6,132,556
Members
449,735
Latest member
Gary_M

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