How do I use square brackets in Application.OnKey? (Excel VBA)

I would like to use the Shift+ Alt+ key combination [to call a subroutine in my VBA project. Here is the code I tried:

Appication.OnKey "+%[", "mySubroutine"

      

I had no problem using other offset and alt characters such as lowercase letters and numbers. However, when I try to use the left square bracket, I get the following error:

Method 'OnKey' of object '_Application' failed

      

I also tried using the left square bracket with all the different combinations Ctrl, Altand Shift. They all throw the same error.

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

      

I also tried to use the ASCII code for the left square bracket (91) like this:

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

      

no luck.

I also tried using the built-in Chr () function of Excel with this ASCII code key:

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

      

which doesn't work either.

I am running Excel 2013. Another computer in our office is running Excel 2003, and although this computer uses the Excel4Macro language, it can use the left square bracket to customize the keyboard shortcut.

It looks like Microsoft has removed this feature in the new version of Excel. However, if anyone can find a way to make it work, I would be grateful!

+3


source to share


1 answer


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

      

Same codes as for SendKeys ...

How to print or send curly 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 the parenthesis "{}". The square brackets "[]" must also be enclosed in parentheses, although they have no special meaning. Use "{{}" and "{}}" to specify parenthesis characters yourself.

+7


source







All Articles