VBA Userform 2
VBA Userform 2
UserForm :We want to show you some real time examples of User form using Excel VBA. These examples will
help you to explore how you can use Excel VBA User form is used to automate different tasks or developing
applications/ tools using VBA User form.
We will take you through different Userform controls. We show you how to create user form in Excel using
VBA. We will continuously put our efforts to build more projects.
Please find more details about VBA ActiveX Label 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.
7. Like this you can add number of Label controls on the UserForm according to your requirement.
Add dynamic Label_Control on the UserForm using VBA
Please find the following steps and example code, it will show you how to add dynamic Label control on the
userform.
End Sub
6. Call the below procedure named ‘Add_Dynamic_Label ’ and find the below procedure to run.
Sub Add_Dynamic_Label()
'Add Dynamic Label and assign it to object 'Lbl'
Set lbl = UserForm2.Controls.Add("Forms.Label.1")
'Label Position
lbl.Left = 10
lbl.Top = 10
End Sub
7. Now, click F5 to run the macro, click ‘Create_Label ’ button to see the result.
8. You can see the created dynamic Label.
Delete Label_Control on the UserForm using VBA
Please find the below code, it will show you how to delete or remove the control on the UserForm. In
the below example, its deleting the Label named ‘New Label’ 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 control During Run Time
Please find more details about VBA ActiveX TextBox 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.
Add ActiveX Dynamic Text Box Control on the UserForm Using VBA
Please find the following steps and example code, it will show you how to add dynamic TextBox control on the
userform.
1. Add Text Box and CommandButton on the userform from the toolbox.
2. Right click on the CommandButton, click properties
3. Change the CommandButton caption to ‘Create_TextBox ’
4. Double click on the CommandButton
5. Now, it shows the following code.
End Sub
6. Call the below procedure named ‘Add_Dynamic_TextBox ’ and find the below procedure to run.
Sub Add_Dynamic_TextBox()
'Add Dynamic TextBox and assign it to object 'Lbl'
Set lbl = UserForm2.Controls.Add("Forms.TextBox.1")
'TextBox Position
lbl.Left = 10
lbl.Top = 10
End Sub
7. Now, click F5 to run the macro, click ‘Create_TextBox ’ button to see the result.
8. You can see the created dynamic Text Box
Please find the below code it will show you how to clear ActiveX Text Box control. In the below
example ‘TextBox1’ is the text box name.
Sub Clr_TxtBx()
TextBox1.Text = ""
End Sub
Please find the below code, it will show you how to delete or remove the control on the UserForm. In
the below example, its deleting the TextBox named ‘New TextBox’ 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 control During Run Time
Code 1: Deleting or Removing Text Box control which is created during run time.
Please find more details about VBA ActiveX Combo Box Control and how we are adding it 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 ComboBox on the Userform from the Toolbox..
4. Double Click on the UserForm, and select the Userform event
End Sub
Code:
7. Now, Press ‘F5’ to run the code. You can see the following Output.
Please find the following steps and example code, it will show you how to add dynamic Combo Box control on
the userform.
End Sub
6. Call the below procedure named ‘Add_Dynamic_ComboBox ’ and find the below procedure to run.
Sub Add_Dynamic_ComboBox()
'Add Dynamic Combo Box and assign it to object 'CmbBx'
Set CmbBx = UserForm3.Controls.Add("Forms.comboBox.1")
7. Now, click F5 to run the macro, click ‘Create_ComboBox ’ button to see the result.
8. You can see the created dynamic combo box..
Please find the following code, it will show you how to add list items to Combo Box.
In the above code ComboBox1 is the name of the Combo Box. Where ‘additem’ is the property of
Combo Box.
Clear Items from the ComboBox_Control using VBA
Please find the following code, it will show you how to clear the Combo Box items. The below code
clears the ComboBox1 items on the UserForm1.
Sub Clr_CmbBx()
UserForm3.ComboBox1.Clear
End Sub
Please find the below code to know how to get the selected value of the Combo Box using VBA. In the
below example value is the property of Combo box.
Sub Chk_Item_SelectOrNot()
MsgBox ComboBox1.Value
End Sub
Here is the VBA Combo Box default values in Excel. After adding items to Combo Box by using any of
the below code you can define the default value.
Code 1:
The below code is useful to select blank option in Combo Box . Where ‘-1’ is the index number.
Sub LstBx_Dflt_Val_Ex1()
UserForm3.ComboBox1.ListIndex = -1
End Sub
Code 2:
The below code is useful to select first item in the Combo Box from the available list. . Where ‘0’ is the
index number.
Sub LstBx_Dflt_Val_Ex2()
UserForm3.ComboBox1.ListIndex = 0
End Sub
Code 3:
The below code is useful to select second item in the Combo Box from the available list. Where ‘1’ is
the index number.
Sub LstBx_Dflt_Val_Ex3()
UserForm3.ComboBox1.ListIndex = 1
End Sub
Code 4:
The below code is useful to select the last item in the Combo Box from the available list. Where ‘1’ is
the index number.
Sub LstBx_Dflt_Val_Ex4()
UserForm3.ComboBox1.ListIndex = UserForm3.ComboBox 1.Count - 1
End Sub
Here is the following example, it will show you how to get the total count of items in a Combo Box. In
the below example ComboBox1 is the Combo Box name and ListCount is the property of Combo Box .
Sub Get_Ttl_Cnt()
MsgBox "Total Items in a ComboBox is " & UserForm3.ComboBox1.ListCount
End Sub
Please find more details about VBA ActiveX ListBox_Control and how we are adding it 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 Listbox_Control on the Userform from the Toolbox.
End Sub
Code:
7. Now, Press ‘F5’ to run the code. You can see the following Output.
Please find the following steps and example code, it will show you how to add dynamic list box control on the
userform.
End Sub
6. Call the below procedure named ‘Add_Dynamic_Listbox’ and find the below procedure to run.
Sub Add_Dynamic_Listbox()
'Add Dynamic List Box and assign it to object 'LstBx'
Set LstBx = UserForm3.Controls.Add("Forms.ListBox.1")
7. Now, click F5 to run the macro, click ‘Create_Listbox’ button to see the result.
8. You can see the created dynamic checkbox.
Add Items to ListBox_Control using VBA
Please find the following code, it will show you how to add list items to list box.
In the above code ListBox1 is the name of the listbox_Control. Where additem is the property of listbox.
Please find the following code, it will show you how to clear the list box items. The below code clears
the list box1 items on the UserForm1.
Sub Clr_LstBx()
UserForm3.ListBox1.Clear
End Sub
Please find the below code to know how to check if a List box is selected or not using VBA. In the
below example (0) is the index number.
Sub Chk_Item_SelectOrNot()
If UserForm3.ListBox1.Selected(0) = True Then
MsgBox "First item has selected in the ListBox."
Else
MsgBox "First item has not selected in the ListBox."
End If
End Sub
Here is the VBA list box default values in Excel. After adding items to list box by using any of the
below code you can define the default value.
Code 1:
The below code is useful to select blank option in list box. Where ‘-1’ is the index number.
Sub LstBx_Dflt_Val_Ex1()
UserForm3.ListBox1.ListIndex = -1
End Sub
Code 2:
The below code is useful to select first item in the list box from the available list. . Where ‘0’ is the
index number.
Sub LstBx_Dflt_Val_Ex2()
UserForm3.ListBox1.ListIndex = 0
End Sub
Code 3:
The below code is useful to select second item in the list box from the available list. Where ‘1’ is the
index number.
Sub LstBx_Dflt_Val_Ex3()
UserForm3.ListBox1.ListIndex = 1
End Sub
Code 4:
The below code is useful to select the last item in the list box from the available list. Where ‘1’ is the
index number.
Sub LstBx_Dflt_Val_Ex4()
UserForm3.ListBox1.ListIndex = UserForm3.ListBox1.Count - 1
End Sub
Here is the following example, it will show you how to get the total count of items in a list box. In the
below example ListBox1 is the list box name and ListCount is the property of list box.
Sub Get_Ttl_Cnt()
MsgBox "Total Items in a ListBox is " & UserForm3.ListBox1.ListCount
End Sub
Move all Items from ListBox1 to ListBox2
Please find the below example code, it shows how to Move all Items from ListBox1 to ListBox2. In the
below example ‘ListBox1 and ListBox2’ are the list box names.
Sub Move_ListBox_Items()
'Variable declaration
Dim iCnt As Integer
Please find the below example code, it shows how to Get Selected Items from ListBox1 to ListBox2. In
the below example ‘ListBox1 and ListBox2’ are the list box names.
Sub Get_ListBox_Selected_Items()
'Variable declaration
Dim iCnt As Integer
Please find the below example code, it shows how to make ListBox to Select Multiple Items. In the
below example ‘ListBox1’ is the list box name.
Sub Multiple_ListBox_Selection()
ListBox1.MultiSelect = fmMultiSelectMulti
End Sub
Populate ListBox from an Array
Please find the below example code, it shows how to populate ListBox from an Array. In the below
example ‘arrList’ is the array name. And ‘ListBox1’ is the list box name.
Sub Load_ListBox_From_Array()
'Load Items to an Array
arrList = Array("Item 1", "Item 2", "Item 3")
1. Go To Developer Tab and then click Visual Basic from the Code or Press Alt+F11.
2. Go To Insert Menu, Click UsereForm. Please find the screenshot for the same.
3. Drag a check box on the Userform from the Toolbox. Please find the screenshot for the same.
4. Now, you can see the following code.
End Sub
Please find the following steps and example code, it will show you how to add dynamic checkbox control on the
userform.
End Sub
6. Call the below procedure named ‘Add_Dynamic_Checkbox’ and find the below procedure to run.
Private Sub CommandButton1_Click()
Call Add_Dynamic_Checkbox
End Sub
Sub Add_Dynamic_Checkbox()
'Add Dynamic Checkbox and assign it to object 'Cbx'
Set Cbx = UserForm2.Controls.Add("Forms.CheckBox.1")
'Checkbox Position
Cbx.Left = 10
Cbx.Top = 10
End Sub
7. Now, click F5 to run the macro, click ‘Create_Checkbox’ button to see the result.
8. You can see the created dynamic check box.:
Please find the below code to know how to select or UnSelect a check box using VBA. In the below example
we are using value property of the check box to select or UnSelect a check box.
Please find the below code to know how to check if a check box is selected or not using VBA. In the below
example we are using value property of the check box.
Sub ChkBox_Ex1()
If CheckBox1.Value = True Then
MsgBox “CheckBox has selected”
Else
MsgBox “CheckBox has not selected”
End If
End Sub
Please find more details about VBA ActiveX Option 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 the Frame and two Option Button controls on the Userform from the Toolbox
Caption: Gender
6. Double click on first OptionButton control, now you will see the following code in the VBA Editor window.
End Sub
Caption: FeMale
9. Double click on second OptionButton control, now you will see the following code in the VBA Editor window.
End Sub
11. You can see the following outputs. It is as shown in the following screen shot.
Please find the following steps and example code, it will show you how to add dynamic Option Button control
on the UserForm.
1. Add Option Button and CommandButton on the userform from the toolbox.
2. Right click on the CommandButton, click properties
3. Change the CommandButton caption to ‘Create_Option Button ’
4. Double click on the CommandButton
5. Now, it shows the following code.
End Sub
6. Call the below procedure named ‘Add_Dynamic_OptionButton ’ and find the below procedure to run.
Sub Add_Dynamic_OptionButton()
'Add Dynamic OptionButton and assign it to object 'OpBtn'
Set OpBtn = UserForm2.Controls.Add("Forms.OptionButton.1")
'OptionButton Position
OpBtn.Left = 15
OpBtn.Top = 10
End Sub
7. Now, click F5 to run the macro, click ‘Create_Option Button ’ button to see the result.
8. You can see the created dynamic Option Button.
Please find the below code, it will show you how to delete or remove the control on the UserForm. In
the below example, its deleting the Option Button named ‘New Option 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 control During Run Time
Private Sub CommandButton1_Click()
'We can use Add method to add the new controls on run time
Set lblBtn = Me.Controls.Add("Forms.Option Button.1")
With lblBtn
.Top = 20
.Left = 20
.Caption = "New Option Button"
.Name = "lblNew1"
End With
MsgBox "New Option Button Added"
End Sub
Code 1: Deleting or Removing Option Button_control which is created during run time.
Here is the below example. It will show you how to Check if a OptionButton is Selected or Not Using
VBA.
Sub Chk_OptBtn_Selection()
If OptionButton1.Value = True Then
MsgBox "OptionButton has Selected"
Else
MsgBox "OptionButton has Not Selected"
End If
End Sub
Option Button and Checkbox both displays an option. Both are used to turn On or Off options.
Difference between is, Option Button is used to select only one selection from multiple selections.
Where as CheckBox is used to select multiple selections from the available list.
Excel VBA UserForm 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.
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.
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.
End Sub
Code:
Private Sub CommandButton1_Click()
MsgBox "Hello!"
End Sub
7. Now, Press ‘F5’ to run the code. You can see the following Output.
Please find the following steps and example code, it will show you how to add dynamic Command Button
control on the userform.
End Sub
6. Call the below procedure named ‘Add_Dynamic_CommandButton ’ and find the below procedure to run.
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
7. Now, click F5 to run the macro, click ‘Create_CommandButton’ button to see the result.
8. You can see the created dynamic Command Button.
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
Please find more details about VBA ActiveX Image_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.
Please find the following steps and example code, it will show you how to add dynamic Image_control on the
userform.
1. Add Image and CommandButton on the userform from the toolbox.
2. Right click on the CommandButton, click properties
3. Change the CommandButton caption to ‘Create_Image ’
4. Double click on the CommandButton
5. Now, it shows the following code.
End Sub
6. Call the below procedure named ‘Add_Dynamic_Image ’ and find the below procedure to run.
Sub Add_Dynamic_Image()
'Add Dynamic Image and assign it to object 'Img'
Set Img = UserForm2.Controls.Add("Forms.Image.1")
With Img
'Load Picture to Image Control
.Picture = LoadPicture("C:\Image Excel ActiveX Control Object.jpg") ‘Change Image Path here
'Image Position
.Left = 50
.Top = 10
End With
End Sub
7. Now, click F5 to run the macro, click ‘Create_Image ’ button to see the result.
8. You can see the created dynamic Image_control
Please find the below code, it will show you how to delete or remove the control on the UserForm. In
the below example, its deleting the Image named ‘New Image’ 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 control During Run Time