EnterKey在VBA用户窗体中按按钮 [英] EnterKey to press button in VBA Userform
问题描述
我在Excel中有一个用户窗体,要求输入用户名和密码.如果您按 Enter 输入密码,它只会选择"下一个项目,即LogIn
按钮,但不会按.您必须再次按下 Enter 才能真正按下按钮.
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 LogIn
button, but it doesn't press it. You have to hit Enter again to actually press the button.
如何做到这一点,当用户按下键盘上的Enter键时,按下登录"按钮,并运行与之关联的代码(Logincode_click
)?
How can I make it so when the user presses enter on his keyboard the LogIn button is pressed and the code associated to is runs (Logincode_click
)?
推荐答案
您还可以使用TextBox的On Key Press事件处理程序:
You could also use the TextBox's On Key Press event handler:
'Keycode for "Enter" is 13
Private Sub TextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
Logincode_Click
End If
End Sub
Textbox1是一个示例.确保选择要引用的文本框,Logincode_Click也是您使用此代码调用(运行)的示例子.确保您引用了首选子
Textbox1 is an example. Make sure you choose the textbox you want to refer to and also Logincode_Click is an example sub which you call (run) with this code. Make sure you refer to your preferred sub
这篇关于EnterKey在VBA用户窗体中按按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!