将热键分配给为 Excel VBA 创建的表单上的按钮
我已经为 Excel 创建了一个宏,它将弹出包含按钮和文本字段的表单。有没有办法为按钮分配“ctrl+Enter”或“F12”等热键?无论焦点位于哪个字段或按钮,热键都应该起作用。
(到目前为止,我已成功创建buttons/fields_Keydowns来检查vbKeyF12的MSForms.ReturnInteger,但我必须对每个字段和按钮执行此操作,有更简单的方法吗?)
比如说,我在表单上有两件事,按钮“ CommandButton1”和文本字段“TextBox1”
按钮的代码:
Private Sub CommandButton1_click()
ActiveCell.FormulaR1C1 = UserForm1.TextBox1.Text
End Sub
当我添加更多字段和按钮时,热键将很有用...
另外,我如何设置“Escape”按钮来关闭/隐藏表单?
I have created a macro for excel which will pop up form that contains button and text field. Is there a way to assign hotkeys such as 'ctrl+Enter' or 'F12' to the buttons? The hotkey should work regardless of which field or button the focus is on.
(so far I have managed to create buttons/fields_Keydowns to check MSForms.ReturnInteger for vbKeyF12, but I have to do that for every field and button, is there an easier way?)
Say, I have 2 things on the form, button "CommandButton1" and textfield "TextBox1"
code for the button:
Private Sub CommandButton1_click()
ActiveCell.FormulaR1C1 = UserForm1.TextBox1.Text
End Sub
The hotkey will be useful when I add in more fields and buttons...
Also how do i set the 'Escape' button to close/hide the form ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要设置“Escape”键来激活按钮以关闭/隐藏表单:
首先,您需要一个按钮,其 Click 事件会隐藏您的表单。然后将该按钮的“取消”属性设置为 True。
显示表单后,按 Esc 键,表单将关闭。
对于“热键”,将按钮的 Accelerator 属性设置为字母,然后当窗体打开时,如果按 Alt+[您的字母],将触发该按钮的 Click 事件。
To set the 'Escape' key to activate your button to close/hide the form:
First, you need a button whose Click event hides your form. Then set the 'Cancel' property of that button to True.
When your form is displayed, and you press Esc, the form will close.
For a 'HotKey', set the Accelerator property of your button to a letter, then when your form is open, if you press Alt+[your letter], the Click event of that button will fire.
唯一的方法是为窗体的每个控件添加一个 KeyUp 事件,该事件调用中央键盘处理例程:
The only way to do that is to add a KeyUp event for each control of your form which calls a central keyboard handling routine: