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 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.

Step 1:

Add your command button as shown in below picture:

command button

Add Button in Excel

Step 2:

Double click on the Button (note: Before double clicking make sure that you are in design mode in excel macro as highlighted above)

Command Button

Add code for Command Button


You can right click on the command button and click on “View Code” as well

View code of Command Button

View code of Command Button

Step 3:

By default you will be taken to a screen as shown below.

Click Event for Command Button

Click Event for Button

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


Join over 10, 000+ Excel VBA Enthusiasts & get this FREE e-Book Now!