{"id":4602,"date":"2016-04-21T14:01:07","date_gmt":"2016-04-21T14:01:07","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/wp\/?p=4602"},"modified":"2022-08-12T11:40:38","modified_gmt":"2022-08-12T11:40:38","slug":"link-macro-to-a-command-button","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/","title":{"rendered":"VBA Beginners: How to write macro for a Command Button"},"content":{"rendered":"<p>Dear Beginners,<\/p>\n<p>Quite a few times I was getting this question from my readers &#8211; <\/p>\n<ul>\n<li>\n<li><strong>How to associate a VBA code to a button?<\/strong><\/li>\n<li><strong>How to write a vba code which gets executed by clicking on a command button in excel vba.<\/strong><\/li>\n<li><strong>Add a button to trigger my macro <\/strong><\/li>\n<li>and so on&#8230;<\/li>\n<\/ul>\n<h2>Here are 3 simple steps by which you can associate a code with a command button:<\/h2>\n<p><i><strong>Note:<\/strong> These steps are common for all the ActiveX Controls added in excel sheet like Text box, Radio Button, Check box etc.<\/i><\/p>\n<h2>Step 1: <\/h2>\n<p>Add your command button as shown in below picture:<br \/>\n<div id=\"attachment_4605\" style=\"width: 523px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-4605\" src=\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-1.png\" alt=\"command button\" width=\"513\" height=\"246\" class=\"size-full wp-image-4605\" \/><p id=\"caption-attachment-4605\" class=\"wp-caption-text\">Add Button in Excel<\/p><\/div><\/p>\n<h2>Step 2:<\/h2>\n<p>Double click on the Button (note: Before double clicking make sure that you are in design mode in excel macro as highlighted above)<br \/>\n<div id=\"attachment_4607\" style=\"width: 585px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-4607\" src=\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-2.png\" alt=\"Command Button\" width=\"575\" height=\"257\" class=\"size-full wp-image-4607\" \/><p id=\"caption-attachment-4607\" class=\"wp-caption-text\">Add code for Command Button<\/p><\/div><\/p>\n<h3>OR<\/h3>\n<p>You can right click on the command button and click on &#8220;View Code&#8221; as well<br \/>\n<div id=\"attachment_4612\" style=\"width: 283px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-4612\" src=\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-2-1.png\" alt=\"View code of Command Button\" width=\"273\" height=\"242\" class=\"size-full wp-image-4612\" \/><p id=\"caption-attachment-4612\" class=\"wp-caption-text\">View code of Command Button<\/p><\/div><\/p>\n<h2>Step 3:<\/h2>\n<p>By default you will be taken to a screen as shown below.<br \/>\n<div id=\"attachment_4608\" style=\"width: 416px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" aria-describedby=\"caption-attachment-4608\" src=\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-3.png\" alt=\"Click Event for Command Button\" width=\"406\" height=\"239\" class=\"size-full wp-image-4608\" \/><p id=\"caption-attachment-4608\" class=\"wp-caption-text\">Click Event for Button<\/p><\/div><br \/>\n<br \/>\nIn the above image as you can see in the right hand side there is a drop down which has all the events which can be triggered on this commandbutton like .&#8221;Click&#8221;, Double Click&#8221;, on focus etc. As you as select an event VBA screen will automatically take you to that event procedure syntax. <\/p>\n<p>There between &#8220;Sub&#8221; and &#8220;End Sub&#8221; you can write your own code or you call call any other function which you have already defined.<\/p>\n<h2>Note: The above 3 steps can be done for any of the ActiveX Controls added in your excel sheet. Like, Text Box, Radio Button, Check box etc.<\/h2>\n<h2> Provide your feedback if you like this simple and crisp information<\/h2>\n<span class=\"et_bloom_bottom_trigger\"><\/span>","protected":false},"excerpt":{"rendered":"<p>Dear Beginners, Quite a few times I was getting this question from my readers &#8211; How to associate a VBA code to a button? How to write a vba code which gets executed by clicking on a command button in excel vba. Add a button to trigger my macro and so on&#8230; Here are 3 [&hellip;]<\/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":"","rop_custom_images_group":[],"rop_custom_messages_group":[],"rop_publish_now":"initial","rop_publish_now_accounts":{"facebook_10221221723514640_204908552914904":"","twitter_aTo1MTIwNTUyMDQ7_512055204":""},"rop_publish_now_history":[],"rop_publish_now_status":"pending","footnotes":""},"categories":[1674,1679,1675],"tags":[],"class_list":["post-4602","post","type-post","status-publish","format-standard","hentry","category-excel-macro-basics","category-excel-macro-beginner","category-excel-macro-for-beginners"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v23.5 (Yoast SEO v23.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Link my VBA code to a command Button<\/title>\n<meta name=\"description\" content=\"How can you link your VBA code or function to a command button so that user can perform the action by clicking on it. Here you can learn about Text Box, Check box, radio button etc. as well.\" \/>\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\/2016\/04\/link-macro-to-a-command-button\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"VBA Beginners: How to write macro for a Command Button\" \/>\n<meta property=\"og:description\" content=\"How can you link your VBA code or function to a command button so that user can perform the action by clicking on it. Here you can learn about Text Box, Check box, radio button etc. as well.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/\" \/>\n<meta property=\"og:site_name\" content=\"Let&#039;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=\"2016-04-21T14:01:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-08-12T11:40:38+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-1.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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/\"},\"author\":{\"name\":\"Vishwamitra Mishra\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"headline\":\"VBA Beginners: How to write macro for a Command Button\",\"datePublished\":\"2016-04-21T14:01:07+00:00\",\"dateModified\":\"2022-08-12T11:40:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/\"},\"wordCount\":323,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"image\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-1.png\",\"articleSection\":[\"Excel Macro Basics\",\"Excel Macro Beginner\",\"Excel Macro Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/\",\"name\":\"Link my VBA code to a command Button\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-1.png\",\"datePublished\":\"2016-04-21T14:01:07+00:00\",\"dateModified\":\"2022-08-12T11:40:38+00:00\",\"description\":\"How can you link your VBA code or function to a command button so that user can perform the action by clicking on it. Here you can learn about Text Box, Check box, radio button etc. as well.\",\"breadcrumb\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#primaryimage\",\"url\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-1.png\",\"contentUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-1.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vmlogger.com\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel Macro Basics\",\"item\":\"https:\/\/vmlogger.com\/excel\/excel-macro-basics\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"VBA Beginners: How to write macro for a Command Button\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\",\"url\":\"https:\/\/vmlogger.com\/excel\/\",\"name\":\"Let&#039;s excel in Excel\",\"description\":\"Let&#039;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 &amp; 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":"Link my VBA code to a command Button","description":"How can you link your VBA code or function to a command button so that user can perform the action by clicking on it. Here you can learn about Text Box, Check box, radio button etc. as well.","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\/2016\/04\/link-macro-to-a-command-button\/","og_locale":"en_US","og_type":"article","og_title":"VBA Beginners: How to write macro for a Command Button","og_description":"How can you link your VBA code or function to a command button so that user can perform the action by clicking on it. Here you can learn about Text Box, Check box, radio button etc. as well.","og_url":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/","og_site_name":"Let&#039;s excel in Excel","article_publisher":"http:\/\/www.facebook.com\/vmlogger","article_author":"http:\/\/www.facebook.com\/vmlogger","article_published_time":"2016-04-21T14:01:07+00:00","article_modified_time":"2022-08-12T11:40:38+00:00","og_image":[{"url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-1.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":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#article","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/"},"author":{"name":"Vishwamitra Mishra","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"headline":"VBA Beginners: How to write macro for a Command Button","datePublished":"2016-04-21T14:01:07+00:00","dateModified":"2022-08-12T11:40:38+00:00","mainEntityOfPage":{"@id":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/"},"wordCount":323,"commentCount":0,"publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"image":{"@id":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#primaryimage"},"thumbnailUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-1.png","articleSection":["Excel Macro Basics","Excel Macro Beginner","Excel Macro Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/","url":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/","name":"Link my VBA code to a command Button","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/#website"},"primaryImageOfPage":{"@id":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#primaryimage"},"image":{"@id":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#primaryimage"},"thumbnailUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-1.png","datePublished":"2016-04-21T14:01:07+00:00","dateModified":"2022-08-12T11:40:38+00:00","description":"How can you link your VBA code or function to a command button so that user can perform the action by clicking on it. Here you can learn about Text Box, Check box, radio button etc. as well.","breadcrumb":{"@id":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#primaryimage","url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-1.png","contentUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2016\/04\/Step-1.png"},{"@type":"BreadcrumbList","@id":"https:\/\/vmlogger.com\/excel\/2016\/04\/link-macro-to-a-command-button\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vmlogger.com\/excel\/"},{"@type":"ListItem","position":2,"name":"Excel Macro Basics","item":"https:\/\/vmlogger.com\/excel\/excel-macro-basics\/"},{"@type":"ListItem","position":3,"name":"VBA Beginners: How to write macro for a Command Button"}]},{"@type":"WebSite","@id":"https:\/\/vmlogger.com\/excel\/#website","url":"https:\/\/vmlogger.com\/excel\/","name":"Let&#039;s excel in Excel","description":"Let&#039;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 &amp; 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\/4602"}],"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=4602"}],"version-history":[{"count":0,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/4602\/revisions"}],"wp:attachment":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/media?parent=4602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/categories?post=4602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/tags?post=4602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}