将热键分配给为 Excel VBA 创建的表单上的按钮

发布于 2024-08-02 14:34:03 字数 463 浏览 4 评论 0原文

我已经为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好倦 2024-08-09 14:34:03

要设置“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.

咿呀咿呀哟 2024-08-09 14:34:03

唯一的方法是为窗体的每个控件添加一个 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

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:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文