我想使用组合键 Shift + Alt + [ 来调用我的 VBA 项目中的子例程。这是我尝试过的代码:
Appication.OnKey "+%[", "mySubroutine"
我在使用其他字符(例如小写字母和数字)与 shift 和 alt 时没有任何问题。但是,当我尝试使用左方括号时,出现以下错误:
Method 'OnKey' of object '_Application' failed
我还尝试将左方括号与所有不同的组合 Ctrl、Alt 和 Shift 结合使用。它们都会产生相同的错误。
Application.OnKey "^[", "mySubroutine"
Application.OnKey "+[", "mySubroutine"
Application.OnKey "%[", "mySubroutine"
Application.OnKey "^+[", "mySubroutine"
Application.OnKey "^%[", "mySubroutine"
Application.OnKey "^+%[", "mySubroutine"
我还尝试使用左方括号 (91) 的 ASCII 代码,如下所示:
Application.OnKey "+%{91}", "mySubroutine"
运气不好。
我还尝试使用 Excel 的内置 Chr() 函数和该 ASCII 键码:
Application.OnKey "+%" & Chr(91), "mySubroutine"
这也不起作用。
我正在运行 Excel 2013。我们办公室的另一台计算机正在运行 Excel 2003,虽然该计算机使用 Excel4Macro 语言,但它可以使用左方括号来设置键盘快捷键。
微软似乎在新版本的 Excel 中删除了此功能。但是,如果有人能找到一种方法使其发挥作用,我将不胜感激!
最佳答案
Application.OnKey "+%{[}", "mySubroutine"
与 SendKeys 相同的代码...
How to print or send braces ( ) using VBA sendkeys
https://social.technet.microsoft.com/wiki/contents/articles/5169.vbscript-sendkeys-method.aspx
The plus sign "+", caret "^", percent sign "%", tilde "~", and parentheses "()" all have special meanings and must be enclosed within braces "{}". Square brackets "[]" must also be enclosed within braces although they have no special meaning. To specify brace characters themselves, use "{{}" and "{}}".
关于vba - 如何在 Application.OnKey 中使用方括号? (Excel VBA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43056916/