{"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":"
Dear Beginners,<\/p>\n
Quite a few times I was getting this question from my readers – <\/p>\n
\n
\n
How to associate a VBA code to a button?<\/strong><\/li>\n
How to write a vba code which gets executed by clicking on a command button in excel vba.<\/strong><\/li>\n
Add a button to trigger my macro <\/strong><\/li>\n
and so on…<\/li>\n<\/ul>\n
Here are 3 simple steps by which you can associate a code with a command button:<\/h2>\n
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
Step 1: <\/h2>\n
Add your command button as shown in below picture: \n
Add Button in Excel<\/p><\/div><\/p>\n
Step 2:<\/h2>\n
Double click on the Button (note: Before double clicking make sure that you are in design mode in excel macro as highlighted above) \n
Add code for Command Button<\/p><\/div><\/p>\n
OR<\/h3>\n
You can right click on the command button and click on “View Code” as well \n
View code of Command Button<\/p><\/div><\/p>\n
Step 3:<\/h2>\n
By default you will be taken to a screen as shown below. \n
Click Event for Button<\/p><\/div> \n \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 .”Click”, Double Click”, on focus etc. As you as select an event VBA screen will automatically take you to that event procedure syntax. <\/p>\n
There between “Sub” and “End Sub” you can write your own code or you call call any other function which you have already defined.<\/p>\n
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
Provide your feedback if you like this simple and crisp information<\/h2>\n<\/span>","protected":false},"excerpt":{"rendered":"
Dear Beginners, Quite a few times I was getting this question from my readers – 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… Here are 3 […]<\/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":[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":"\n
Link my VBA code to a command Button<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n