VBA Beginners: How to write macro for a Command Button
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 simple steps by which you can associate a code with a command button:
Note: These steps are common for all the ActiveX Controls added in excel sheet like Text box, Radio Button, Check box etc.
Add your command button as shown in below picture:
Double click on the Button (note: Before double clicking make sure that you are in design mode in excel macro as highlighted above)
You can right click on the command button and click on “View Code” as well
By default you will be taken to a screen as shown below.
In 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.
There between “Sub” and “End Sub” you can write your own code or you call call any other function which you have already defined.
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.
Provide your feedback if you like this simple and crisp information
My name is Vishwamitra Mishra. Friends Call me Vishwa. This blog is authored by me. I am an Excel Geek. Well, this blog talks a lot about my passion in Excels & Macros so I’ll not talk about it :) I am very much passionate about traveling & quite recently discovered that I am a good photographer too..:P
AND GET A FREE!
E-BOOK FOR EXCEL VBA BEGINNERS
DON'T MISS ANY NEW ARTICLE !
Get your FREE! E-Book & Have Every New Article Delivered Straight To Your Email-Box