You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel VBA中实现按回车键触发命令按钮点击功能

Excel VBA用户窗体:回车键触发按钮点击异常问题

我编写了一段用于通过UserForm输入用户名/密码的Excel VBA代码,点击命令按钮时运行正常,但希望同时实现按回车键触发命令按钮的点击事件。该代码通过标签标题判断当前处于用户名输入还是密码输入状态,且会验证用户名是否为合法邮箱地址。我希望实现点击命令按钮或按回车键均可触发操作,但目前按回车键需要点击两次才能生效。

原有代码

Private Const EMAIL_REGEX As String = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$"
Private IsEnteringUsername As Boolean

Private Sub CBUserName_Click()
    Dim regex As Object
    Set regex = CreateObject("VBScript.RegExp")

    regex.Global = True
    regex.IgnoreCase = True
    regex.Pattern = EMAIL_REGEX

    If regex.Test(UsernamePasswordTextBox.Value) Then
        M8Username = UsernamePasswordTextBox.Value
        LbUserPass.Caption = "Please enter your Password"
        CBUserName.Visible = False
        CBPassWord.Visible = True
        UsernamePasswordTextBox.Value = ""
        UsernamePasswordTextBox.PasswordChar = "*"
        UsernamePasswordTextBox.SetFocus
    Else
        MsgBox "Invalid Username!"
        UsernamePasswordTextBox.Value = ""
        UsernamePasswordTextBox.SetFocus
    End If

    Set regex = Nothing
End Sub

Private Sub CBPassWord_Click()
    M8Password = UsernamePasswordTextBox.Value
    LbUserPass.Caption = "Please enter your Username"
    CBUserName.Visible = True
    CBPassWord.Visible = False
    UsernamePasswordTextBox.PasswordChar = ""
    Me.Hide
    GetJobs
End Sub

Private Sub UserForm_Activate()
    UsernamePasswordTextBox.Value = ""
    LbUserPass.Caption = "Please enter your Username"
    UsernamePasswordTextBox.SetFocus
    CBUserName.Visible = True
    CBPassWord.Visible = False
    Me.Left = Application.Left + (0.5 * Application.Width) - (0.5 * Me.Width)
    Me.Top = Application.Top + (0.5 * Application.Height) - (0.5 * Me.Height)
    IsEnteringUsername = True
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = VbQueryClose.vbFormControlMenu Then
        MsgBox "You must enter a valid Username & Password to use this program"
        Cancel = True
    End If
End Sub

Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Dim regex As Object

    If KeyCode = KeyCodeConstants.vbKeyReturn Then
        Set regex = CreateObject("VBScript.RegExp")
        regex.Global = True
        regex.IgnoreCase = True
        regex.Pattern = EMAIL_REGEX

        If IsEnteringUsername Then
            If regex.Test(UsernamePasswordTextBox.Value) Then
                M8Username = UsernamePasswordTextBox.Value
                LbUserPass.Caption = "Please enter your Password"
                CBUserName.Visible = False
                CBPassWord.Visible = True
                UsernamePasswordTextBox.Value = ""
                UsernamePasswordTextBox.PasswordChar = "*"
                UsernamePasswordTextBox.SetFocus
            Else
                MsgBox "Invalid Username!"
                UsernamePasswordTextBox.Value = ""
                UsernamePasswordTextBox.SetFocus
            End If
        Else
            M8Password = UsernamePasswordTextBox.Value
            LbUserPass.Caption = "Please enter your Username"
            CBUserName.Visible = True
            CBPassWord.Visible = False
            UsernamePasswordTextBox.PasswordChar = ""
        End If

        Set regex = Nothing
    End If
End Sub

新编写的代码

Private Sub UsernamePasswordTextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
   
    If KeyCode = KeyCodeConstants.vbKeyReturn Then
        If LbUserPass.Caption = "Please enter your Username" Then
            Call CBUserName_Click
        Else
            Call CBPassWord_Click
        End If
    End If

End Sub

内容的提问来源于stack exchange,提问作者Hareborn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 18:23:18