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.
On opening in VBE, you have to go to Insert and then UserForm has to be inserted, as can be seen in the image.
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.
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.
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.
Now we will insert text box in UserForm, by going to toolbox we will select text box option and then drag into UserForm.
Now we will insert a button in the UserForm, After insert we will go to properties then its name will be ‘Show text’.
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.
Now you can see in the sheet that our form has been created,
Fill the value in the text box, then click on the show text button, we will get the message.
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.
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.
Now you can see in the sheet that our form has been created, you can close the UserForm by clicking on the close button.
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.
After this we will drag the button and assign it to the macro. Also rename the button.
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.
After clicking show button we will get message something like this
Now we will close this UserForm. We can also use shape, image or icon like button here, let’s insert a shape.
Select the shape then drag and name the button and then customize it according to the font color of the font.
Assign the Macro.
If we click on the Insert button, then we will open the UserForm.
Write text in text box then click show button we will get message.
We can close by clicking close button.
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>>
You can also see well-explained video here about How to Use Command Button in UserForm in Excel VBA