自定义VBA UserForm窗体内的热键

我想让我的用户窗体closures(或其他命令),而不使用加速键。

例如,当用户按F4时从Excel中打开下面的表单。 我也想用F4来closures(Unload Me)。

这是我目前正在使用的,虽然它似乎不必要的大:

Private Sub TextBoxA_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxB_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxC_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxD_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxE_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxF_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxG_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxH_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxI_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxJ_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxK_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxM_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxN_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Private Sub TextBoxO_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) moveme Val(keycode) End Sub Sub moveme(keycode As Integer) If keycode = 115 Then Unload Me End Sub 

我想我正在寻找这样的东西,但我不知道:

 loop: Private Sub TextBox[i]_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) Sub moveme(keycode As Integer) If keycode = 115 Then Unload Me End Sub 

在Access中,您可以直接将函数放入控件的事件代码框中,其中包括多个控件的相同function。

在Excel和Word中,我认为唯一的方法是使用WithEvents。 这里有一些关于这个主题的链接。 如果这还不够,Google“Excel WithEvents”。 (WithEvents是一个词。)

Chip Pearson的网站

Mr.Excel论坛

每个文本框都需要自己的事件处理程序,出于多种原因,同样,当我明白你想要做什么时,你不能在程序外执行语句,所以你不能像创build一系列Private Sub TextBox[i]...在程序之外。

幸运的是,使用Application.OnKey可能会有更简单的方法。

在标准模块中,如果您有一个用于初始化表单的macros,请执行以下操作:

 Sub showform() 'Displays/initializes the form and assigns hotkey function to F4 Application.OnKey "{F4}", "CloseForm" UserForm1.Show vbModeless End Sub Sub CloseForm() 'Sub to close the userform when F4 is pressed Unload UserForm1 'optionally, revert F4 to its normal behavior 'Application.OnKey "{F4}" 'reset F4 to open the form: Application.OnKey "{F4}", "showform" End Sub 

如果您已经使用F4来打开表单,那么您需要稍微调整逻辑,例如检查表单是否已经显示,或者是否有error handling等。

有一些这样的 vbModeless 不会工作,而forms有焦点,因此不会工作,除非forms显示vbModeless 。 这不适用于显示vbModal的表单,因为在以这种方式显示表单时,只有表单事件才会被识别,所以如果不从窗体对象的事件处理程序调用热键,则不会触发closuresfunction。

否则,我认为你坚持处理每个表单对象的事件的方式,或者,可能探索WithEvents选项。

大卫和皮特给了我一些很好的研究的见解,所以我已经标记皮特是正确的 – 先生论坛链接是非常有帮助的。 为了代码,我结束了以下,但没有标记为正确的,因为可能对其他用户来说太窄。

Userform1代码

 Dim TBs() As New TBClass Private Sub UserForm_Initialize() Dim TBCount As Integer Dim Ctrl As Control TBCount = 0 For Each Ctrl In Absence_Viewer.Controls If TypeName(Ctrl) = "TextBox" Then TBCount = TBCount + 1 ReDim Preserve TBs(1 To TBCount) Set TBs(TBCount).TBGroup = Ctrl End If Next Ctrl 'Do other stuff End Sub 

类名为TBClass的模块代码

 Public WithEvents TBGroup As MSForms.TextBox Private Sub TBGroup_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer) If keycode = 115 Then Unload Userform1 End Sub