为为 excel vba 创建的表单上的按钮分配热键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1340084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:44:05  来源:igfitidea点击:

Assigning hotkeys to buttons on forms created for excel vba

excelvbauserform

提问by Yoga

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.

我为 excel 创建了一个宏,它将弹出包含按钮和文本字段的表单。有没有办法为按钮分配诸如“ctrl+Enter”或“F12”之类的热键?无论焦点在哪个字段或按钮上,热键都应该起作用。

(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?)

(到目前为止,我已经设法创建按钮/字段_Keydowns 来检查 MSForms.ReturnInteger 的 vbKeyF12,但我必须为每个字段和按钮都这样做,有没有更简单的方法?)

Say, I have 2 things on the form, button "CommandButton1" and textfield "TextBox1"

说,我在表单上有 2 个东西,按钮“CommandButton1”和文本字段“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 ?

另外我如何设置“退出”按钮来关闭/隐藏表单?

回答by Stewbob

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.

要设置“Escape”键以激活您的按钮以关闭/隐藏表单:首先,您需要一个按钮,其 Click 事件会隐藏您的表单。然后将该按钮的“取消”属性设置为 True。

When your form is displayed, and you press Esc, the form will close.

当您的表单显示并按 Esc 时,该表单将关闭。

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.

对于“HotKey”,将按钮的 Accelerator 属性设置为一个字母,然后当您的表单打开时,如果您按 Alt+[您的字母],将触发该按钮的 Click 事件。

回答by jkpieterse

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:

唯一的方法是为表单的每个控件添加一个 KeyUp 事件,它调用中央键盘处理例程:

Option Explicit

Private Sub CommandButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    HandleKey KeyCode
End Sub

Private Sub CommandButton2_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    HandleKey KeyCode
End Sub

Private Sub UserForm_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    HandleKey KeyCode
End Sub

Private Sub HandleKey(KeyCode As MSForms.ReturnInteger)
    MsgBox KeyCode
    Select Case KeyCode
    Case 112 'F1
        'Do something because F1 was pressed
    Case 113 'F2
        'Do something because F2 was pressed
    Case 114 'F3 etc.
    End Select
End Sub