How to create a command button and assign macro in Excel VBA ?

  1. Click on the Developer tab View tab.
  2. Click on Insert and Click on Command Button in the ActiveX Controls section
    Developer Tab

This will create a command button within current sheet. Let’s now create a macro and assign it to this command button.

To write a macro within the command button, right click on command button and click view code

view code

3. The Visual Basic Editor will be opened. Place you cursor between

Private Sub CommandButton1_Click()

End Sub

4. For example, add the following code line:

Range(“A1”).Value = “My First Code !”

We just wrote one line code within the command button click event. So whenever you click this command button this code will be executed.

my first code

As per the written code, the command button will write a value in cell A1 as “My First Code !” as shown in the above screenshot.

You can use multiple command buttons to execute multiple repetitive tasks or create a complete form like interface within the sheet.

Thank you for reading. Happy Learning !

Please leave a reply if you like the article or if you need any help on excel

%d bloggers like this: