The following code line runs:
Application.OnKey “&”, “macro name”
But this line
Application.OnKey “(“, “macro name”
causes runtime error 1004:
Application-defined or object-defined error
Why? The only difference is the character. I'm using Excel on a Mac.
It seems that for whatever reason brackets are considered to be special characters, to use them to trigger the Application.OnKey you have to put them inside curly braces:
Sub SetupKeyBindings()
Application.OnKey "{(}", "TestMacro"
Application.OnKey "{)}", "TestMacro"
MsgBox "Key bindings set. Try pressing Shift+9/0 to run the macro."
End Sub
Sub TestMacro()
MsgBox "TestMacro was triggered!"
End Sub
Instead of curly braces you can also use Application.OnKey "+9" for Shift-9
Edit, you can also use key codes with the curly braces ("{key_code}"), the full list can be found here.
For example Application.OnKey "{106}" captures the numpad multiplication sign as opposed to the normal multiplication sing (Shift-8).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With