{"id":12175,"date":"2013-06-08T19:21:54","date_gmt":"2013-06-08T19:21:54","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=2949"},"modified":"2013-06-08T19:21:54","modified_gmt":"2013-06-08T19:21:54","slug":"assign-a-shortcut-key-using-excel-vba","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/","title":{"rendered":"Assign a Shortcut Key using Excel VBA"},"content":{"rendered":"
Dear LEM Readers<\/strong>, OnKey is an Application method which makes a particular procedure run in excel vba when a specific key or combination is pressed.<\/p>\n Syntax:<\/font><\/strong> Where:<\/strong> To assign a Shortcut Key CTRL + b<\/strong> for a Subroutine or procedure named MyProgram<\/strong> then execute the below code and you are done…<\/p>\n Syntax:<\/font><\/strong> Example:<\/font><\/strong><\/p>\n To assign a Shortcut Key CTRL + b<\/strong> for a Subroutine or procedure named MyProgram<\/strong> then execute the below code and you are done…<\/p>\n <\/p>\n
\n
\nIn the previous article, you learnt how to assign or modify a shortcut key using excel option. In this article you will learn how to assign shortcut keys to your already written procedure or subroutine using VBA (excel macro). At the end of this article you will be comfortable enough with association shortcut keys for your written macros. But it is a best practice to assign a shortcut key using excel option because when there is a simple option available in excel to do this then why to write code and execute it. This article I am posting for you to know that “Yes it is possible using excel VBA code as well.”
\n
\nThere are two ways of assigning a shortcut key to a macro in excel vba. One is using Application.OnKey<\/strong> and other one is using Application.MacroOptions<\/strong>. Here in this article we are going to learn both the techniques.
\n <\/p>\nMethod 1 : Assign a Shortcut Key using OnKey in Excel VBA<\/h2>\n
\nApplication.OnKey, <Key as String><\/i> , < Procedure><\/i><\/font><\/p>\n
\nKey as String:<\/font> is the Key combination you want to assign to your procedure.
\nProcedure:<\/font> is the name of the procedure which you want to be run.
\n
\nExample:<\/font><\/strong><\/p>\n\nApplication.OnKey \"^b\", \"MyProgram\"\n<\/code><\/pre>\n
\nAs soon as you execute the above statement, Shortcut Key CTRL + b<\/strong> will be assigned to the macro named “MyProgram” and pressing the key combination <\/strong>CTRL + b<\/strong> will trigger the procedure MyProgram<\/strong>
\n
\nIn the above example you can see I have used <\/strong>Caret Sign (^)<\/strong> before the letter b<\/strong>. Caret Sign is used for CTRL<\/strong> Key. Similarly percentage sign %<\/strong> is used for ALT<\/strong> Key and Plus Sign +<\/strong> is used for Shift Key<\/strong>.<\/font><\/i>
\n <\/p>\nMethod 2 : Assign a Shortcut Key Application.MacroOptions<\/h2>\n
\n
\nApplication.MacroOptions Macro:=”<Macro Name>”, Description:=”<Description of Macro>”, HasShortcutKey:=True, ShortcutKey:=”<Your Shortcut Key>”<\/font><\/p>\n\nApplication.MacroOptions macro:=\"MyProgram\", Description:=\"Description of the Macro\", _\nhasshortcutkey:=True, ShortcutKey:=\"^b\"\n<\/code><\/pre>\n
\nAs soon as you execute the above statement, Shortcut Key CTRL + b<\/strong> will be assigned to the macro named “MyProgram” and pressing the key combination <\/strong>CTRL + b<\/strong> will trigger the procedure MyProgram<\/strong>
\n
\nIn the above example you can input the description of the Shortcut Key as well.<\/font><\/i><\/p>\nAdvantage of Using VBA Code for assigning a Shortcut Key<\/h2>\n