I want to start up the UserForm with ONLY pressing on a number and on Enter (so no clicking involved). The problem I have is with pressing Enter twice. What happens if I press E
Just set Doorgaan
to Default
within the properties
in the VBE. There is only one element per form that can be default
. The default element will be activated upon pressing Enter
. So, if you set that button to Default = True
then the code for that button will run when you press enter.
You can also change the element which is Default
during runtime like so:
UserForm1.btnDoorgaan.Default = True
By the way, there is also the option to set an element to Cancel = True
. That element gets activated when you hit Escape
on the key board. So, if you add a Cancel
button to your form and assign it to Cancel = True
then you can easily close and exit that form by just pressing Escape
.
Ralph's solution is the easiest way to implement it if you have a simple form like yours
If you have multiple buttons, or use labels instead of buttons you can do something like this
Private Sub TxtBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = vbKeyReturn Then Me.Hide 'or CommandButton1_Click
End Sub