Activate UserForm with only one 'Enter'

前端 未结 2 936
忘掉有多难
忘掉有多难 2021-01-22 05:36

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

相关标签:
2条回答
  • 2021-01-22 06:24

    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.

    0 讨论(0)
  • 2021-01-22 06:34

    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
    
    0 讨论(0)
提交回复
热议问题