Where are user keyboard shortcuts stored

nbrcrunch

Well-known Member
Joined
Jan 1, 2003
Messages
2,062
I tried searching this website for "shortcuts file" or "shortcuts storage" and found nothing.

When writing VBA, I would like to specify a shortcut. I've noticed on recorded macros the line that Excel puts in that is just a comment line acknowledging the shortcut exists. But where is this stored? Or at least, how do I create one myself without having to use the Macro menu option?
 
Andrew,

I took your snippet of code and pasted it into my previous example.
When I ran it, I got error 1004, Method....Failed.

Here is what I have:
____________________

Sub hello()

Application.MacroOptions Macro:="MyMacro", HasShortcutKey:=True, ShortcutKey:="Z"

MsgBox ("Hello Word")

End Sub
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
That's probably because you don't have a Macro called "MyMacro". My snippet was just a generic example - change Macro argument so that it's the name of your Macro.
 
Upvote 0
nbrcrunch said:
Andrew,

I took your snippet of code and pasted it into my previous example.
When I ran it, I got error 1004, Method....Failed.

Here is what I have:
____________________

Sub hello()

Application.MacroOptions Macro:="MyMacro", HasShortcutKey:=True, ShortcutKey:="Z"

MsgBox ("Hello Word")

End Sub

Hi, the code does work. The issue is that you are trying to assign a description & a shortcut key to a non-existent macro. The macro called MyMacro doesnt exist.

Try this instead...

Code:
Sub hello()
Application.MacroOptions Macro:="hello", HasShortcutKey:=True, ShortcutKey:="Z"
MsgBox ("Hello Word")
End Sub
 
Upvote 0
The Short cut Keys or Accellerator key for macros are STORED
within the Macro routines code.....you just don't see it, as it is an Attribute of the Modules code......

The Attribute for short cut key Ctrl + z is

Code:
Attribute YourMacroName.VB_ProcData.VB_Invoke_Func = "z\n14"

Where YourMacroname = the name of your Macro
z in "z\n14" is the short cut letter

A capital letter in "S\n14" denotes a Ctrl + Shift + s key combination
 
Upvote 0
Ivan F Moala said:
The Short cut Keys or Accellerator key for macros are STORED
within the Macro routines code.....you just don't see it, as it is an Attribute of the Modules code......

The Attribute for short cut key Ctrl + z is

Code:
Attribute YourMacroName.VB_ProcData.VB_Invoke_Func = "s\n14"

Where YourMacroname = the name of your Macro
s in "s\n14" is the short cut letter

A capital letter in "S\n14" denotes a Ctrl + Shift + s key combination

Ivan,

So is there some code that returns the ShortcutKey assigned to a Macro?
 
Upvote 0
I'm not 100% sure...haven't really played with it much...but I guess one way would be to Export the code and grab the text strings.......
 
Upvote 0
Ivan, in your very informative msg below, you say that the attribute for Ctrl-z is... "s/n14"

But then the followup reply indicates that is for the letter S. I am confused about the syntax "s/n14" What is the breakdown of that syntax.

Also, I tried copying the snippet to my code and got an "expected expression" compile error:

Sub hello()
Application.MacroOptions Macro:="hello", HasShortcutKey:=True, ShortcutKey:="Z"
Attribute hello.VB_ProcData.VB_Invoke_Func = "s\n14"
MsgBox ("Hello Word")
End Sub

I got the 2nd line working as far as assignment goes but I'm not sure what the Attribute statement is supposed to do.

Ivan F Moala said:
The Short cut Keys or Accellerator key for macros are STORED
within the Macro routines code.....you just don't see it, as it is an Attribute of the Modules code......

The Attribute for short cut key Ctrl + z is

Code:
Attribute YourMacroName.VB_ProcData.VB_Invoke_Func = "s\n14"

Where YourMacroname = the name of your Macro
s in "s\n14" is the short cut letter

A capital letter in "S\n14" denotes a Ctrl + Shift + s key combination
 
Upvote 0
nbrcruch, if you have a macro that you want to assign a shortcut key to you can use the OnKey method. For syntax just search the help files in the VBA editor for OnKey, there is a whole page on the usage.


Example from the help file:
This example assigns "InsertProc" to the key sequence CTRL+PLUS SIGN and assigns "SpecialPrintProc" to the key sequence SHIFT+CTRL+RIGHT ARROW.

Application.OnKey "^{+}", "InsertProc"
Application.OnKey "+^{RIGHT}", "SpecialPrintProc"

This example returns SHIFT+CTRL+RIGHT ARROW to its normal meaning.

Application.OnKey "+^{RIGHT}"




HT
 
Upvote 0

Forum statistics

Threads
1,215,479
Messages
6,125,043
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top