带有“%a”击键的Application.OnKey错误

时间:2017-04-19 15:02:15

标签: vba excel-vba excel

我在'ThisWorkbook'对象中有以下代码:

Sub Workbook_Open()
    Application.OnKey "^a", "SS_1"
    Application.OnKey "^%a", "SS_1"
    Application.OnKey "%a", "SS_1"
End Sub

在单独的模块中,我有以下代码:

Sub SS_1()
MsgBox "Hello"
End Sub

"^a""^%a"击键都完美地执行SS_1子,并且会出现MsgBox。

但是,"%a"按键似乎什么也不做,也没有显示消息框。

是否有任何我不知道的独特因素会导致此击键操作与其他击键操作不同?

提前感谢您的帮助,

斯科特

1 个答案:

答案 0 :(得分:1)

这对我有用:

Option Explicit

Public Sub TestMe()

    Application.OnKey "^a", "Macro1"
    Application.OnKey "^%a", "Macro2"
    Application.OnKey "%a", "Macro3"

    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",false)"

End Sub

Public Sub Macro1()

    Debug.Print "Pryc"

End Sub

Public Sub Macro2()

    Debug.Print "Pryc2"

End Sub

Public Sub Macro3()

    Debug.Print "Pryc3"

End Sub

Public Sub UnTestMe() 'Run this to disable the shortcuts.

    Application.OnKey "^a"
    Application.OnKey "^%a"
    Application.OnKey "%a"

    Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",true)"

End Sub

编辑: 为了确保有效,您应该隐藏功能区。否则,它会在功能区上找到一些内容,即A,并将其用作屏幕截图。

尝试此操作以隐藏功能区: Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",false)"

尝试此操作将其返回: Application.ExecuteExcel4Macro "SHOW.TOOLBAR(""Ribbon"",true)"