2017-03-27 235 views
3

我想使用組合鍵 + Alt鍵+[調用子程序在我的VBA項目。這是我曾嘗試代碼:如何在Application.OnKey中使用方括號? (EXCEL VBA)

我一直在使用其它字符轉變和alt,如小寫字母和數字沒有問題。然而,當我嘗試使用左方括號,我得到以下錯誤:

Method 'OnKey' of object '_Application' failed 

我也曾嘗試使用左方括號與所有不同的組合按CtrlAlt鍵,和Shift。他們都產生相同的錯誤。

Application.OnKey "+%{91}", "mySubroutine" 

沒有運氣:

Application.OnKey "^[", "mySubroutine" 
Application.OnKey "+[", "mySubroutine" 
Application.OnKey "%[", "mySubroutine" 
Application.OnKey "^+[", "mySubroutine" 
Application.OnKey "^%[", "mySubroutine" 
Application.OnKey "^+%[", "mySubroutine" 

我也使用ASCII代碼用於左括號(91)像這樣嘗試。

我用Excel內置的CHR()函數與ASCII鍵碼也試過:

Application.OnKey "+%" & Chr(91), "mySubroutine" 

,沒有工作,要麼。

我正在運行Excel 2013.我們辦公室中的另一臺計算機正在運行Excel 2003,儘管該計算機使用的是Excel4Macro語言,但它能夠使用左方括號來設置鍵盤快捷鍵。

似乎微軟在較新版本的Excel中刪除了此功能。但是,如果任何人都能想出辦法讓它工作,我將不勝感激!

回答

7
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 "{}}".

+0

真棒!非常感謝! (爲什麼我沒有想到...) – mattbierwirth