CommandButton

CommandButton is one of the UserForm control. You can select and drag CommandButton on the UserForm. 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 or UserForm. 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 UserForm or Worksheet.

PREMIUM TEMPLATES LIMITED TIME OFFER

ON SALE80% OFF

50+ Project Management Templates Pack
Excel PowerPoint Word

Advanced Project Plan & Portfolio Template
Excel Template

Business Presentations Templates Pack
PowerPoint Slides

20+ Excel Project Management Pack
Excel Templates

20+ PowerPoint Project Management Pack
PowerPoint Templates

10+ MS Word Project Management Pack
Word Templates


[/fusion_toggle][/fusion_accordion]

VBA ActiveX CommandButton Control on the UserForm

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

  1. Go To Developer Tab and then click Visual Basic from the Code or Press Alt+F11.
  2. Go To Insert Menu, Click UserForm. Please find the screenshot for the same.
  3. Drag a CommandButton on the Userform from the Toolbox. Please find the below screenshot for the same.
  4. Now double click on the Command Button, which is dragged on the UserForm .
  5. Now you can see the following code in the VBA Editor window.
  6. Private Sub CommandButton1_Click()
    End Sub
    
  7. Now add the following code to the in between above procedure.
  8. Code:

    Private Sub CommandButton1_Click()
    MsgBox "Hello!"
    End Sub
    
  9. Now, Press ‘F5’ to run the code. You can see the following Output. It is shown in the following Screen Shot.
  10. output:

Add dynamic CommandButton Control on the UserForm using VBA

Please find the following steps and example code, it will show you how to add dynamic Command Button control on the userform.

  1. Add command button on the userform from the toolbox.
  2. Right click on the command button, click properties
  3. Change the command button caption to ‘Create_CommandButton’
  4. Double click on the command button
  5. Now, it shows the following code.
  6. Private Sub CommandButton1_Click()
    End Sub
    
  7. Call the below procedure named ‘Add_Dynamic_CommandButton ’ and find the below procedure to run.
  8. Private Sub CommandButton1_Click()
    Call Add_Dynamic_CommandButton 
    End Sub
    

    Procedure to call in the Command Button :

    Sub Add_Dynamic_CommandButton()
    'Add Dynamic CommandButton and assign it to object 'CmdBtn'
    Set CmdBtn = UserForm2.Controls.Add("Forms.CommandButton.1")
    With CmdBtn
    'Assign CommandButton Name
    CmdBtn.Caption = "Dynamic CommandButton"
    'CommandButton Position
    .Left = 12
    .Top = 10
    .Width = 102
    End With
    End Sub
    
  9. Now, click F5 to run the macro, click ‘Create_CommandButton’ button to see the result.
  10. You can see the created dynamic Command Button which is shown in the following screen shot.
  11. output:

    Delete CommandButton Control on the UserForm using VBA

    Please find the below code, it will show you how to delete or remove a command button on the UserForm. In the below example, its deleting the command button named ‘New Button’ which is on the UserForm named ‘UserForm4’. We can use Remove method to delete the controls which are created during run time. Controls which are created during design time cannot be deleted using this method. Please find the below example and screen shots for better understand.
    Code 1: Adding CommandButton During Run Time

    Private Sub CommandButton1_Click()
    'We can use Add method to add the new controls on run time
    Set CmdBtn = Me.Controls.Add("Forms.CommandButton.1")
    With CmdBtn
    .Top = 20
    .Left = 20
    .Caption = "New Button"
    .Name = "cmdNew1"
    End With
    MsgBox "New button Added"
    End Sub
    

    Please find the below screen shot for your reference for the above macro and its output.
    When we click on Add Command Button:

    Code 2: Deleting or Removing CommandButton which is created during run time.

    Private Sub CommandButton2_Click()
    'We can use Remove method to delete the controls which are created during run time
    'Note: Controls which are created on design time can not be deleted using this method
    Me.Controls.Remove ("cmdNew1")
    MsgBox "New button Deleted"
    End Sub
    

    Please find the below screen shot for your reference for the above macro and its output.
    When we click on Delete Command Button:

    [/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

    LIMITED TIME OFFER

Related pages


definition of row and column in ms excelcomment excel vbasql interview questions and answers in pdffile exists vbscriptarray function in vbarecordset in vbavba application.vlookupjoin rows in excelscreen update vbaresource capacity planning excel templateexcel numerical orderhow to make a dropdown list in excel 2010excel vba count rowsadd drop down list in excel 2007merge sheets excelvba environmentapplication function vbamso file viewerschedule in excel templatehow to start vba in excelremove excel duplicatesformula match exceldynamic dashboard in excelhow to refresh a pivot tablesales dashboard templatedml statements in sql servercreating excel macrosql frequently asked interview questions and answershow do i do a pivot table in excel 2010vlookup and hlookup in excel 2007 with examplehyperlink codingexcel remove duplicates from columnhr interview questions indiabixif functions in excel 2010vba checkboxcreating a scorecard in excelexcel 2007 create drop down listaverageifs exampleisempty in vbahow to adjust the column width in excelmessagebox syntaxhow to create a data entry form in excel 2007declaring arrays in vbaconcatenation excelremove hyperlinks excel 2007sql sort commanddml examplesexcel vba hide worksheetautomatic hyperlink excelmicrosoft excel template downloadshlookup definitionexcel vba tableexcel 2013 remove password protectionhow to find password of protected sheet in excelexcel.workbooks.addhow to delete duplicates in excel 2013how to print cells in excelconnect excel to sql serverexcel advanced filter criteriadml sqlhow to delete numbers in excelvba save workbookvba datevaluesyntax for sql commandshow to use the countif function in excel 2013excel vba hide columnsexcel to csv converterdefine vlookuphow to change all column widths in excelduplicate function in excelexcel 2007 sumifhow to protect excel worksheet with passwordhlookup excel 2013arrange ascending orderunprotect workbook without passworddim worksheetvba development toolsnested if and statements excelproject plan timeline template excelexcel offset function