How to Add VBA/Excel Command Button

The VBA/Excel command button is an ActiveX control. Command button is used to perform certain task as a user clicks on it.

Create interactive Excel spreadsheets with VBA Command Buttons.

What is the command button in Excel?

  • The VBA/Excel command button is an ActiveX control.
  • The command button is used to perform a certain task as a user clicks on it.
  • You may run a macro or execute the procedure, trigger message box, etc. as the user clicks the button.
  • Placing a command button in an Excel sheet is a matter of simple "three-click" work.

Where command button is located?

If you are working in VBA for Excel then you might already add the Developer tab in the ribbon. If not, you may add this by:

File --> Options --> Customize Ribbon --> Developer

Once the Developer tab is added, you may see the insert group there as shown in the image below:

VBA-developer-tab

Adding the ActiveX button

Step 1

In the developer tab, click on the “Insert” button group:

VBA-insert-group

Step 2

Under the ActiveX controls heading, locate the command button as shown below:

VBA Command Button placeholder ready to be customized.

This should be the first one towards the left.

Step 3

Click on the command button and now click the mouse somewhere on the Sheet where you want to add the button.

Drag the + sign and make the button as big as you want.

That’s it!

How to add code in the button

To make the button functional, you need to add some code to perform a certain task.

To add the code, you may double-click the button that you just added.

It should take you to the VBA code editor.

An example of assigning text as a button is clicked

Let us perform some action as a user clicks on the button.

The task is to add some text to the B3 cell as the button is single-clicked.
Step 1:

Add a button to the Excel sheet:

Example of a VBA Command Button with 'Click Me' label

Step 2:

Ensure that “Design Mode” is selected as shown below:

VBA-design-mode

Step 3:

Double-click on the button that should lead you to the VBA editor as shown below:

VBA-code

Step 4:

Write this line of code:

Range("B3").Value = "testing"

Save this code and come back to the Excel sheet.

Step 5:

Unselect the “Design Mode” as shown below:

VBA-design-unselect

Step 6:

Click on the button and it should assign “testing” to the B3 cell as shown below:

Connecting a VBA Command Button to a macro in the Visual Basic Editor.

An example of triggering a message box

In this example, we attached a message box to the button. As you click on the button, the message box will appear with a simple message and an OK button.

The following line of code is written in the button click event:

MsgBox ("Welcome to VBA/Excel!")

In the VBA code editor:

VBA-Excel-Msgbox

As you click the button, the following message box appears:

Connecting a VBA Command Button to display a message box in Excel

How to edit the caption of the button

In the first example, you saw as we added the button, it has the following default caption (display  text): CommandButton1

You may change the button caption easily.

1st way:

Right-click on the button and you will see:

“CommandButton Object” option

Click on Edit option as shown below

button-label

The prompt will be at the button label – write the text that you want to appear for the button.

2nd Way:

Right-click on the button and you will see the “Properties” option there.

Clicking it will open the Properties window. There locate “caption” and change the text as you want. See the image below for the steps:

Image of VBA editor showing How to edit the caption of the button