CommandButton is used to run or execute a macro or procedure. It performs a task or an action when a user clicks on a command button. Command Button can be used on the WorkSheet. Please find more details about ActiveX CommandButton Control in the following chapter. You can see how we are adding or deleting command button on the Worksheet.

VBA ActiveX CommandButton Control on the Worksheet

Please find more details about VBA ActiveX Command Button Control on the Worksheet.

  1. Go To Developer Tab and then click Insert from the Controls group.
  2. Click on the Command Button from the ActiveX Controls group.
  3. Drag a Command Button on the Worksheet.
  4. Right click on the Command Button, before that Design Mode should be turned ON.
  5. Click on properties from the available list.
  6. On the left side find ‘Caption’ from the available properties of a Command Button .
  7. On the right side mention as ‘Click’. Please find the below screen shot for the same.
  8. CommandButton Excel VBA ActiveX Control2

  9. Now, Double click on the Command Button (Click). Or right click on command button and click view code.
  10. Now you can see the following code in the VBA Editor window.
  11. Private Sub CommandButton21_Click()
    End Sub
    
  12. Now add the following code in between above procedure.
  13. Code:

    Private Sub CommandButton21_Click()
    MsgBox "Hi, Welcome to Analysistabs"
    End Sub
    
  14. Now, Press ‘F5’ to run the code. You can see the following Output. It is shown in the following Screen Shot.
  15. output:

    CommandButton Excel VBA ActiveX Control66

Delete CommandButton Control on the Worksheet using VBA

Please find the below code, it will show you how to delete or remove a command button on the worksheet. In the below example, its deleting the command button named ‘CommandButton1’ which is on the worksheet named ‘Sheet11’. Here Delete is the property of Shape object.

Sub Delete_CommandButton()
Sheets("Sheet11").Shapes("CommandButton1").Delete
End Sub

Hey! Join Our Community

Get Quick Responses & Experts' Answers in Minutes!
Get Notified - When Answered Your Question!