How to Use Command Button in UserForm in Excel VBA Easily

Welcome to Excel Avon

Command Button in UserForm

DOWNLOAD USED EXCEL FILE FROM HERE>>

In today’s post, we will tell you how to use Command Button in UserForm in Excel VBA, in previous post that Category: Excel VBA Macros Introduction to UserForm VBA. In this article, we will Use Command Button in UserForm in Excel VBA. Command Button is one of the UserForm controls. You can select and drag the Command Button onto the UserForm. 

Command Buttons are used to run or execute a macro or procedure. When the user clicks on a command button, it performs a task or action. Command buttons can be used on a worksheet or UserForm. You can see how we are adding or removing command buttons on UserForm or Worksheet.

Create Command Button in UserForm

Well, you should know how to insert UserForm, because in the previous article we introduced you to UserForm and told you how to insert UserForm in VBE. Let us understand. How to insert USERFORM, then we have to go like last time, first go to Developer Tab then click on Visual Basic’s Option as shown in the image below.

sort-data-on-excel-using-VBA

On opening in VBE, you have to go to Insert and then UserForm has to be inserted, as can be seen in the image.

introduction-of-userform

When we click on the UserForm, a separate wizard box will be created in a window VBE, in which you can see UserForm1 as the default name.

introduction-of-userform.png

If you look under the add-ins tab in VBE, you will see the option of tools, you have to open the toolbox by clicking on it.

CAMMAND-BUTTON-IN-userform6 (21)

Now we will change the name of the label, here is the properties option from where you can change the name (text) by going to the caption option.

How-to-Use-Command-Button-in-UserForm-in Excel VBA

Now we will insert text box in UserForm, by going to toolbox we will select text box option and then drag into UserForm.

How-to-Use-Command-Button-in-UserForm

Now we will insert a button in the UserForm,  After insert we will go to properties then its name will be ‘Show text’.

How-to-Use-Command-Button-in-UserForm2

Now we will double click on the command button, then something like this will happen where we will have to add some functions for which we are using the command button. This code is because when we will fill some text in the text box and click on show text button then we will get the message whatever we have written and here we have used the message box function.

Private Sub CommandButton1_Click()
MsgBox "you have writtten '" & Me.TextBox1.Value & "'"
End Sub

Now we will Click On Run Button.

introduction-of-userform6 (6)1 

Now you can see in the sheet that our form has been created,

How-to-Use-Command-Button-in-UserForm3

Fill the value in the text box, then click on the show text button, we will get the message.

how-to-use=coommand-button-in=excel

Now we will create a command button for close which will close the UserForm. let’s go to VBE. then we’ll go to the tool box then we’ll do the command button.

how-to-use=coommand-button-in=excel.png

Now we will double click on the Close button, use the unload function.

Private Sub CommandButton2_Click()
Unload.me
End Sub

Now we will Click On Run Button.

introduction-of-userform6 (6)1 

Now you can see in the sheet that our form has been created, you can close the UserForm by clicking on the close button.

how-to-use=coommand-button-in=exce12

Now we will insert module and write subroutine in it.

Sub showUserForm()

End Sub

Now we will use show function for UserForm.

Sub showUserForm()
UserForm1.Show

End Sub

Now we will go to Excel Sheet insert a button for run Macro. 

how-to-use=coommand-button-in=exce14

After this we will drag the button and assign it to the macro. Also rename the button.

how-to-use=coommand-button-in=exce14.png

If we click on the insert button, then we will open the UserForm where we will write the text in the textbox and then we will click on the show button.

how-to-use=coommand-button-in=exce1212.png

After clicking show button we will get message something like this

how-to-use-coommand-button-in-Excel

Now we will close this UserForm. We can also use shape, image or icon like button here, let’s insert a shape.

how-to-use-coommand-button-in-Excel1211

Select the shape then drag and name the button and then customize it according to the font color of the font.

how-to-use-coommand-button-in-Excel

Assign the Macro.

how-to-use-coommand-button-in

If we click on the Insert button, then we will open the UserForm.

how-to-use-cammand-button-in2

Write text in text box then click show button we will get message.

how-to-use=coommand-button-in=exce12

We can close by clicking close button.

how-to-use-coommand-button-in-Excel

Therefore, I hope that you have understood How to Use Command Button in UserForm in Excel VBA, maybe if you do not understand some options, then you can comment us, which we will answer soon and for more information, you can follow us on Twitter, Instagram, LinkedIn and you can also follow on YouTube.

DOWNLOAD USED EXCEL FILE FROM HERE>>

LEARN MORE TOPIC IN VBA HERE

You can also see well-explained video here about How to Use Command Button in UserForm in Excel VBA

Leave a Reply