EnterKey to press button in VBA Userform

后端 未结 6 995
春和景丽
春和景丽 2020-12-31 04:45

I have a userform in Excel that asks for a username and password. Once you enter your password if you press Enter it just \"selects\" the next item which is the <

6条回答
  •  清酒与你
    2020-12-31 05:15

    Be sure to avoid "magic numbers" whenever possible, either by defining your own constants, or by using the built-in vbXXX constants.

    In this instance we could use vbKeyReturn to indicate the enter key's keycode (replacing YourInputControl and SubToBeCalled).

       Private Sub YourInputControl_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
            If KeyCode = vbKeyReturn Then
                 SubToBeCalled
            End If
       End Sub
    

    This prevents a whole category of compatibility issues and simple typos, especially because VBA capitalizes identifiers for us.

    Cheers!

提交回复
热议问题