ActiveX controls – CommandButton in Excel VBA

Inserting Command Button (ActiveX Control) in Excel SpreadSheet

The command button is a ActiveX control which will be used to run written scripts. VBA Command Button can be embedded in both the Excel spreadsheet and the UserForm. How to insert a CommandButton? 

Go to the Developer’s tab in excel window, then insert the ActiveX controls and select the command button. After inserting chceck the properties window. In this window you can check name, that will be used, when you will be programming this button. In this Example the name is CommandButton1. Text on the button you can edit also in properties window in the Caption field.

Inserting Command Button (ActiveX Control) in Excel SpreadSheet

Inserting Command Button (ActiveX Control) in Excel User Form

How to insert a Command Button in VBA Excel UserForm? Choose Command Button control from ToolBox in VBA Editor. Then place it on your UserForm. Text in Command Button you can edit Caption field in Label Properties.

Inserting Command Button (ActiveX Control) in Excel User Form

Programming VBA CommandButton (ActiveX Control) 

The button can be programmed by clicking it in the design mode. The code is edited in the code of the sheet object. In this case it will be Sheet1 or UserForm when you use User forms. In the most cases you will be using action Click. You can also use double click or other actions like mouse-mooving or key-pressing.

Programming VBA CommandButton (ActiveX Control) 
Private Sub CommandButton1_Click()

   Range("A1") = "Text inserted by the button"
   MsgBox ("How Command Button works")
End Sub

VBA CommandButton Properties

In Command Button properties window you can edit many parameters of button, that you inserted into Excel Sheet or User Form. You can change for example:

  • (name) – name using in programming Command Button
  • Caption – Text written on the button
  • Font – Font and text style
  • Colors – Freground, Background
  • Position
VBA CommandButton Properties

File with examples:

More examples of Excel VBA basics can be found in the Excel VBA Course section and  Excel VBA Examples section.

Leave a comment

Your email address will not be published. Required fields are marked *

%d bloggers like this: