{"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>,
\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>\n

Method 1 : Assign a Shortcut Key using OnKey in Excel VBA<\/h2>\n

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>
\nApplication.OnKey, <Key as String><\/i> , < Procedure><\/i><\/font><\/p>\n

Where:<\/strong>
\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

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

\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>\n

Method 2 : Assign a Shortcut Key Application.MacroOptions<\/h2>\n

Syntax:<\/font><\/strong>
\n
\nApplication.MacroOptions Macro:=”<Macro Name>”, Description:=”<Description of Macro>”, HasShortcutKey:=True, ShortcutKey:=”<Your Shortcut Key>”<\/font><\/p>\n

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

\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>\n

<\/p>\n

Advantage of Using VBA Code for assigning a Shortcut Key<\/h2>\n

As you have seen in the previous article to assign Shortcut key in excel options<\/a>, there you can assign only a key which can be typed in the Option box. It means all the alphabets and numbers you can assign as a Shortcut key but what about special keys on the keyboard which can not be typed like Backspace, Enter Key, Delete Key<\/strong> etc.?? Using the VBA code you can assign these special keys as a shortcut key for your procedure.
\n 
\nBelow table gives you the Key Name<\/strong> for all the special keys on the keyboard which needs to be passed in your VBA code<\/font><\/i>
\n<\/p>\n

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Key (Special Key)<\/td>\nKey Code<\/strong><\/font><\/td>\n

<\/strong><\/tr>\n

BACKSPACE<\/td>\n{BACKSPACE} or {BS}<\/td>\n<\/tr>\n
ENTER (From Numeric Keypad)<\/td>\n{ENTER}<\/td>\n

<\/strong><\/tr>\n

BREAK<\/td>\n{BREAK}<\/td>\n<\/tr>\n
ENTER<\/td>\n~ (tilde)<\/td>\n

<\/strong><\/tr>\n

CAPSLOCK<\/td>\n{CAPSLOCK}<\/td>\n<\/tr>\n
ESCAPE Key<\/td>\n{ESCAPE} or {ESC}<\/td>\n

<\/strong><\/tr>\n

CLEAR<\/td>\n{CLEAR}<\/td>\n<\/tr>\n
HELP<\/td>\n{HELP}<\/td>\n

<\/strong><\/tr>\n

DELETE<\/td>\nDELETE or DEL<\/td>\n<\/tr>\n
HOME<\/td>\n{HOME}<\/td>\n

<\/strong><\/tr>\n

DOWN Arrow<\/td>\n{DOWN}<\/td>\n<\/tr>\n
INS (Insert Key)<\/td>\n{INSERT}<\/td>\n

<\/strong><\/tr>\n

END<\/td>\n{END}<\/td>\n<\/tr>\n
LEFT ARROW<\/td>\n{LEFT}<\/td>\n

<\/strong><\/tr>\n

NUM LOCK<\/td>\n{NUMLOCK}<\/td>\n<\/tr>\n
PAGE DOWN<\/td>\n{PGDN}<\/td>\n

<\/strong><\/tr>\n

PAGE UP<\/td>\n{PGUP}<\/td>\n<\/tr>\n
RETURN<\/td>\n{RETURN}<\/td>\n

<\/strong><\/tr>\n

RIGHT ARROW<\/td>\n{RIGHT}<\/td>\n<\/tr>\n
SCROLL LOCK<\/td>\n{SCROLLLOCK}<\/td>\n

<\/strong><\/tr>\n

TAB Key<\/td>\n{TAB}<\/td>\n<\/tr>\n
UP ARROW<\/td>\n{UP}<\/td>\n

<\/strong><\/tr>\n

F1, F2, F3…. F15<\/td>\n{F1}, {F2}, {F3},…..,{F15}<\/td>\n<\/tr>\n<\/table>\n<\/div>\n

How to de-assign or release a Shortcut Key which already assigned using vba code<\/h2>\n

You can pass empty or blank procedure name in the above code to release or de-assign the shortcut key. So if we want to release the Shortcut key CTRL + b<\/strong> which is assigned in the above code, you can run the below statement:<\/p>\n

\n' Procedure Name is passed as blank to release CTRL + b\n\nApplication.OnKey \"^b\", \"\"\n<\/code><\/pre>\n

<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":"

Dear LEM Readers,   In 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 […]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1673,1246,1674,1675,1682],"tags":[],"class_list":["post-12175","post","type-post","status-publish","format-standard","hentry","category-excel-functions","category-macro","category-excel-macro-basics","category-excel-macro-for-beginners","category-popular-articles"],"yoast_head":"\nAssign a Shortcut Key using Excel VBA - Let's excel in Excel<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Assign a Shortcut Key using Excel VBA\" \/>\n<meta property=\"og:description\" content=\"Dear LEM Readers,   In 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 […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/\" \/>\n<meta property=\"og:site_name\" content=\"Let's excel in Excel\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:author\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:published_time\" content=\"2013-06-08T19:21:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/vmlogger.com_-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Vishwamitra Mishra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/www.twitter.com\/learnexcelmacro\" \/>\n<meta name=\"twitter:site\" content=\"@learnexcelmacro\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishwamitra Mishra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/\"},\"author\":{\"name\":\"Vishwamitra Mishra\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"headline\":\"Assign a Shortcut Key using Excel VBA\",\"datePublished\":\"2013-06-08T19:21:54+00:00\",\"dateModified\":\"2013-06-08T19:21:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/\"},\"wordCount\":687,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"articleSection\":[\"Excel Functions\",\"Excel Macro\",\"Excel Macro Basics\",\"Excel Macro Tutorial\",\"Popular Articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/\",\"name\":\"Assign a Shortcut Key using Excel VBA - Let's excel in Excel\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\"},\"datePublished\":\"2013-06-08T19:21:54+00:00\",\"dateModified\":\"2013-06-08T19:21:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vmlogger.com\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel Functions\",\"item\":\"https:\/\/vmlogger.com\/excel\/excel-functions\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Assign a Shortcut Key using Excel VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\",\"url\":\"https:\/\/vmlogger.com\/excel\/\",\"name\":\"Let's excel in Excel\",\"description\":\"Let's share knowledge\",\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/vmlogger.com\/excel\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\",\"name\":\"Vishwamitra Mishra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"contentUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"width\":528,\"height\":560,\"caption\":\"Vishwamitra Mishra\"},\"logo\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\"},\"description\":\"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.\",\"sameAs\":[\"http:\/\/www.learnexcelmacro.com\",\"http:\/\/www.facebook.com\/vmlogger\",\"https:\/\/x.com\/https:\/\/www.twitter.com\/learnexcelmacro\",\"https:\/\/www.youtube.com\/c\/VMLogger\"],\"url\":\"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Assign a Shortcut Key using Excel VBA - Let's excel in Excel","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/","og_locale":"en_US","og_type":"article","og_title":"Assign a Shortcut Key using Excel VBA","og_description":"Dear LEM Readers,   In 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 […]","og_url":"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/","og_site_name":"Let's excel in Excel","article_publisher":"http:\/\/www.facebook.com\/vmlogger","article_author":"http:\/\/www.facebook.com\/vmlogger","article_published_time":"2013-06-08T19:21:54+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/vmlogger.com_-1.png","type":"image\/png"}],"author":"Vishwamitra Mishra","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/www.twitter.com\/learnexcelmacro","twitter_site":"@learnexcelmacro","twitter_misc":{"Written by":"Vishwamitra Mishra","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/#article","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/"},"author":{"name":"Vishwamitra Mishra","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"headline":"Assign a Shortcut Key using Excel VBA","datePublished":"2013-06-08T19:21:54+00:00","dateModified":"2013-06-08T19:21:54+00:00","mainEntityOfPage":{"@id":"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/"},"wordCount":687,"commentCount":4,"publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"articleSection":["Excel Functions","Excel Macro","Excel Macro Basics","Excel Macro Tutorial","Popular Articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/","url":"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/","name":"Assign a Shortcut Key using Excel VBA - Let's excel in Excel","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/#website"},"datePublished":"2013-06-08T19:21:54+00:00","dateModified":"2013-06-08T19:21:54+00:00","breadcrumb":{"@id":"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/vmlogger.com\/excel\/2013\/06\/assign-a-shortcut-key-using-excel-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vmlogger.com\/excel\/"},{"@type":"ListItem","position":2,"name":"Excel Functions","item":"https:\/\/vmlogger.com\/excel\/excel-functions\/"},{"@type":"ListItem","position":3,"name":"Assign a Shortcut Key using Excel VBA"}]},{"@type":"WebSite","@id":"https:\/\/vmlogger.com\/excel\/#website","url":"https:\/\/vmlogger.com\/excel\/","name":"Let's excel in Excel","description":"Let's share knowledge","publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vmlogger.com\/excel\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5","name":"Vishwamitra Mishra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/","url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","contentUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","width":528,"height":560,"caption":"Vishwamitra Mishra"},"logo":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/"},"description":"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.","sameAs":["http:\/\/www.learnexcelmacro.com","http:\/\/www.facebook.com\/vmlogger","https:\/\/x.com\/https:\/\/www.twitter.com\/learnexcelmacro","https:\/\/www.youtube.com\/c\/VMLogger"],"url":"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/"}]}},"_links":{"self":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/12175"}],"collection":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/comments?post=12175"}],"version-history":[{"count":0,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/12175\/revisions"}],"wp:attachment":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/media?parent=12175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/categories?post=12175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/tags?post=12175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}