Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
58 views

VBA Userform 2

The document provides information about using different controls on Excel VBA userforms, including labels, textboxes, and combo boxes. It includes steps to add these controls dynamically using VBA code and also how to clear, delete, and remove controls from a userform.

Uploaded by

giriraj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

VBA Userform 2

The document provides information about using different controls on Excel VBA userforms, including labels, textboxes, and combo boxes. It includes steps to add these controls dynamically using VBA code and also how to clear, delete, and remove controls from a userform.

Uploaded by

giriraj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

VBA Userform

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.

Excel VBA UserForm Label


Label is one of the UserForm control. You can select and drag Label on the UserForm. Label is used to display
show text or information. It can be title, caption, etc. It can be used on the UserForm. You can see how it works
and more details about Userform Label Control on the UserForm in the following chapter.

VBA ActiveX Label_Control on the UserForm

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.

3. Drag the label control on the Userform from the Toolbox.

4. Click on the properties.


5. On the left side find ‘Caption’ from the available properties.
6. On the right side mention as ‘Welcome!’.

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.

1. Add Label and CommandButton on the userform from the toolbox.


2. Right click on the CommandButton, click properties
3. Change the CommandButton caption to ‘Create_Label ’
4. Double click on the CommandButton
5. Now, it shows the following code.

Private Sub CommandButton1_Click()

End Sub

6. Call the below procedure named ‘Add_Dynamic_Label ’ and find the below procedure to run.

Private Sub CommandButton1_Click()


Call Add_Dynamic_Label
End Sub

Procedure to call in the CommandButton:

Sub Add_Dynamic_Label()
'Add Dynamic Label and assign it to object 'Lbl'
Set lbl = UserForm2.Controls.Add("Forms.Label.1")

'Assign Label Name


lbl.Caption = "Dynamic Label"

'Label Border Style


lbl.BorderStyle = 2

'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

Private Sub CommandButton1_Click()


'We can use Add method to add the new controls on run time
Set lblBtn = Me.Controls.Add("Forms.Label.1")
With lblBtn
.Top = 20
.Left = 20
.Caption = "New Label"
.Name = "lblNew1"
End With
MsgBox "New Label Added"
End Sub

When we click on Add Command Button:

Code 1: Deleting or Removing label_control 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 cannot be deleted using this method
Me.Controls.Remove ("lblNew1")
MsgBox "New Label Deleted"
End Sub

When we click on Delete Command Button:

Excel VBA UserForm Textbox


TextBox is one of the UserForm control. You can select and drag TextBox on the UserForm. It is used to
display text or edit existing text on the TextBox. TextBox can have static data or dynamic data. You can see
how TextBox works and more details about UserForm TextBox Control on the UserForm in the following
section.
VBA TextBox Control on the UserForm

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.

3. Drag the TextBox control on the Userform from the Toolbox.


4. Click on the properties.
5. On the left side find ‘Text’ from the available properties.
6. On the right side mention as ‘Welcome!’.
7. Like this you can add number of Text Box controls on the UserForm according to your requirement.

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.

Private Sub CommandButton1_Click()

End Sub

6. Call the below procedure named ‘Add_Dynamic_TextBox ’ and find the below procedure to run.

Private Sub CommandButton1_Click()


Call Add_Dynamic_TextBox
End Sub

Procedure to call in the CommandButton:

Sub Add_Dynamic_TextBox()
'Add Dynamic TextBox and assign it to object 'Lbl'
Set lbl = UserForm2.Controls.Add("Forms.TextBox.1")

'Assign TextBox Name


lbl.Caption = "Dynamic TextBox"

'TextBox Border Style


lbl.BorderStyle = 2

'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

Clear ActiveX TextBox Control Using VBA

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

Delete TextBox 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 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

Private Sub CommandButton1_Click()


'We can use Add method to add the new controls on run time
Set lblBtn = Me.Controls.Add("Forms.TextBox.1")
With lblBtn
.Top = 20
.Left = 20
.Caption = "New TextBox"
.Name = "lblNew1"
End With
MsgBox "New TextBox Added"
End Sub

When we click on Add Command Button:

Code 1: Deleting or Removing Text Box control 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 cannot be deleted using this method
Me.Controls.Remove ("lblNew1")
MsgBox "New TextBox Deleted"
End Sub

When we click on Delete Command Button:

Excel VBA UserForm ComboBox


ComboBox is one of the UserForm control. You can select and drag drop control on the UserForm. This control
is used to store and display list of items to a list. This can be used on the UserForm. Please find more details
about ComboBox Control in the following chapter. You can see how to load items to a Combo Box, how to get
the value of combo box items, etc..,

VBA ComboBox_Control on the UserForm

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

5. Now can see the following code in the module.

Private Sub UserForm_Initialize()

End Sub

6. Now, add the following code to the in between procedure.

Code:

Private Sub UserForm_Initialize()


ComboBox1.AddItem "MBA"
ComboBox1.AddItem "MCA"
ComboBox1.AddItem "MSC"
ComboBox1.AddItem "MECS"
ComboBox1.AddItem "CA"
End Sub

7. Now, Press ‘F5’ to run the code. You can see the following Output.

Add dynamic ComboBox_Control on the UserForm using VBA

Please find the following steps and example code, it will show you how to add dynamic Combo Box 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_ComboBox ’
4. Double click on the command button
5. Now, it shows following code.

Private Sub CommandButton1_Click()

End Sub

6. Call the below procedure named ‘Add_Dynamic_ComboBox ’ and find the below procedure to run.

Private Sub CommandButton1_Click()


Call Add_Dynamic_ComboBox
End Sub

Procedure to call in the Command Button :

Sub Add_Dynamic_ComboBox()
'Add Dynamic Combo Box and assign it to object 'CmbBx'
Set CmbBx = UserForm3.Controls.Add("Forms.comboBox.1")

'Combo Box Position


CmbBx.Left = 20
CmbBx.Top = 10
End Sub

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..

Add Items to ComboBox_Control using VBA

Please find the following code, it will show you how to add list items to Combo Box.

Private Sub Insert _Items _To_ComboBox ()


ComboBox1.AddItem "Item 1"
ComboBox1.AddItem "Item 2"
ComboBox1.AddItem "Item 3"
ComboBox1.AddItem "Item 4"
ComboBox1.AddItem "Item 5"
End Sub

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

Get the selected value of the ComboBox using VBA

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

VBA ComboBox Default Values in Excel

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

Get the total count of ComboBox Items

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

Excel VBA UserForm Listbox


ListBox is one of the UserForm control. You can select and drag ListBox on the UserForm. This control is used
to display list of items to a list. This is used on the UserForm. Please find more details about ListBox_Control in
the following chapter. You can see how to load items to listbox_Control, how to move items from one listbox to
another listbox, how to select items from a listbox_Control, etc..,

VBA ListBox_Control on the UserForm

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.

4. Double Click on the UserForm, and select the Userform event .

5. Now can see the following code in the module.

Private Sub UserForm_Initialize()

End Sub

6. Now, add the following code to the in between procedure.

Code:

Private Sub UserForm_Initialize()


ListBox1.AddItem "MBA"
ListBox1.AddItem "MCA"
ListBox1.AddItem "MSC"
ListBox1.AddItem "MECS"
ListBox1.AddItem "CA"
End Sub

7. Now, Press ‘F5’ to run the code. You can see the following Output.

Add dynamic ListBox_Control on the UserForm using VBA

Please find the following steps and example code, it will show you how to add dynamic list box 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_Listbox’
4. Double click on the command button
5. Now, it shows following code.

Private Sub CommandButton1_Click()

End Sub

6. Call the below procedure named ‘Add_Dynamic_Listbox’ and find the below procedure to run.

Private Sub CommandButton1_Click()


Call Add_Dynamic_Listbox
End Sub

Procedure to call in the Command Button:

Sub Add_Dynamic_Listbox()
'Add Dynamic List Box and assign it to object 'LstBx'
Set LstBx = UserForm3.Controls.Add("Forms.ListBox.1")

'List Box Position


LstBx.Left = 20
LstBx.Top = 10
End Sub

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.

Private Sub Insert _Items _To_LstBox ()


ListBox1.AddItem "Item 1"
ListBox1.AddItem "Item 2"
ListBox1.AddItem "Item 3"
ListBox1.AddItem "Item 4"
ListBox1.AddItem "Item 5"
End Sub

In the above code ListBox1 is the name of the listbox_Control. Where additem is the property of listbox.

Clear Items from the ListBox using VBA

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

Check if a List box item is selected or not using VBA

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

VBA ListBox Default Values in Excel

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

Get the total count of Listbox Items

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

'Moving ListBox1 Items to ListBox2


For iCnt = 0 To ListBox1.ListCount - 1
ListBox2.AddItem ListBox1.List(iCnt)
Next iCnt

'Then Clear the ListBox1 Items


ListBox1.Clear
End Sub

Get Selected Items from ListBox1 to ListBox2

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

'Get Selcted Items from ListBox1 to ListBox2


For iCnt = 0 To ListBox1.ListCount - 1
'Check ListBox Item has selcted or not
If ListBox1.Selected(iCnt) = True Then
ListBox2.AddItem ListBox1.List(iCnt)
End If
Next
End Sub

Make ListBox to Select Multiple Items

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")

'Load an Array Items to ListBox


ListBox1.List = arrList
End Sub

Excel VBA UserForm CheckBox


CheckBox is one of the UserForm control. You can select and drag CheckBox on the UserForm. CheckBox
Control is used to specify or indicate binary choice. That is either Turn on or off a value. When we use more
checkboxs, you can select more than one CheckBox at a time on the UserForm. You can select multiple check
boxes in a group box. Please find more details about CheckBox Control in the following chapter.

VBA ActiveX CheckBox 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 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.

Private Sub CheckBox1_Click()

End Sub

Note: In the above code ‘CheckBox1’ is the Check box name.

5. Please add the following statements to the procedure.

Private Sub CheckBox1_Click()


If UserForm2.CheckBox1.Value = True Then
MsgBox "Checkbox has Checked", , "Checkbox"
Else
MsgBox "Checkbox has UnChecked", , "Checkbox"
End If
End Sub

6. Run the above macro by pressing F5.


7. Check and uncheck the check box twice to get the two different outputs.

Add dynamic CheckBox Control on the UserForm using VBA

Please find the following steps and example code, it will show you how to add dynamic checkbox 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_Checkbox’
4. Double click on the command button
5. Now, it shows following code.

Private Sub CommandButton1_Click()

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

Procedure to call in the Command Button:

Sub Add_Dynamic_Checkbox()
'Add Dynamic Checkbox and assign it to object 'Cbx'
Set Cbx = UserForm2.Controls.Add("Forms.CheckBox.1")

'Assign Checkbox Name


Cbx.Caption = "Checkbox2"

'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.:

Select or UnSelect a CheckBox using VBA?

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.

‘ To select check box


CheckBox1.Value=True

‘ To unselect check box


CheckBox1.Value=False

Check if a check box is selected or not using VBA

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

Excel VBA UserForm OptionButton


OptionButton is one of the UserForm control. You can select and drag OptionButton on the UserForm. This
Control is used to select only one selection from multiple selections within a group. When we select an
OptionButton_Control on the user form in a group immediately it will de select all other OptionButtons in the
same group. It has value property. It indicates whether the OptionButton_Control is selected or not. When we
select Option Button the value is ‘True’. And when it de selects the value is ‘False’. Option Button is used on
the UserForm. You can see how it works and more details about Option Button Control on the UserForm in the
following chapter.

VBA OptionButton_Control on the UserForm

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

4. Select Frame and change its properties as follows.

Caption: Gender

5. Select first OptionButton and change its properties as mentioned below.


Caption: Male

6. Double click on first OptionButton control, now you will see the following code in the VBA Editor window.

Private Sub OptionButton1_Click()

End Sub

7. Now add the following statement to the above procedure.

Private Sub OptionButton1_Click()


MsgBox "Selected Gender is Male"
End Sub

8. Now, Select second OptionButton and change its properties as follows.

Caption: FeMale

9. Double click on second OptionButton control, now you will see the following code in the VBA Editor window.

Private Sub OptionButton2_Click()

End Sub

10. Now add the following statement to the above procedure.

Private Sub OptionButton2_Click()


MsgBox "Selected Gender is FeMale"
End Sub

11. You can see the following outputs. It is as shown in the following screen shot.

Output1: When we select ‘Male’ Option Button

Output2: When we select ‘FeMale’ Option Button

Add dynamic OptionButton_Control on the UserForm using VBA

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.

Private Sub CommandButton1_Click()

End Sub

6. Call the below procedure named ‘Add_Dynamic_OptionButton ’ and find the below procedure to run.

Private Sub CommandButton1_Click()


Call Add_Dynamic_OptionButton
End Sub

Procedure to call in the CommandButton:

Sub Add_Dynamic_OptionButton()
'Add Dynamic OptionButton and assign it to object 'OpBtn'
Set OpBtn = UserForm2.Controls.Add("Forms.OptionButton.1")

'Assign OptionButton Name


OpBtn.Caption = "Dynamic OptionButton"

'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.

Delete OptionButton_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 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

When we click on Add Command Button:

Code 1: Deleting or Removing Option Button_control 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 cannot be deleted using this method
Me.Controls.Remove ("lblNew1")
MsgBox "New Option Button Deleted"
End Sub

When we click on Delete Command Button:

Check if a OptionButton is Selected or Not Using VBA

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

Difference Between OptionButton and CheckBox Control

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.

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.

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.

Private Sub CommandButton1_Click()

End Sub

6. Now add the following code to the in between above procedure.

Code:
Private Sub CommandButton1_Click()
MsgBox "Hello!"
End Sub

7. Now, Press ‘F5’ to run the code. You can see the following 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.

Private Sub CommandButton1_Click()

End Sub

6. Call the below procedure named ‘Add_Dynamic_CommandButton ’ and find the below procedure to run.

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

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.

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

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

When we click on Delete Command Button:


Excel VBA UserForm Image
Image is one of the UserForm control. You can select and drag Image on the UserForm. You can select and
drag Image on the UserForm. Image control embeds a picture such as a jpg, jpeg, gif, png, bitmap, etc. It can be
used on the UserForm. You can see how it works and more details about Image Control.

VBA Image_Control on the UserForm

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.

3. Drag the Image_control on the Userform from the Toolbox.

4. Click on the image_control properties.


5. On the left side find ‘Picture’ from the available properties of the control.
6. On the right side click on that, and select image from the source.
7. On the left side find ‘PictureSizeMode’ from the available properties of the control.
8. On the right side, select ‘1 – frmPictureSiseModeStretch’ from the available list.
9. Now, Click ‘F5’ to see the output.
10. Now, you can see the following output as shown below in the screen shot.

Add dynamic Image_Control on the UserForm using VBA

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.

Private Sub CommandButton1_Click()

End Sub

6. Call the below procedure named ‘Add_Dynamic_Image ’ and find the below procedure to run.

Private Sub CommandButton1_Click()


Call Add_Dynamic_Image
End Sub

Procedure to call in the CommandButton:

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

'Align the Picture Size


.PictureSizeMode = fmPictureSizeModeStretch

'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

Delete Image_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 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

Private Sub CommandButton1_Click()


'We can use Add method to add the new controls on run time
Set lblBtn = Me.Controls.Add("Forms.Image.1")
With lblBtn
.Top = 20
.Left = 40
.Name = "lblNew1"
End With
MsgBox "New Image Control Added"
End Sub

When we click on Add Command Button:

Code 1: Deleting or Removing Image_control 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 cannot be deleted using this method
Me.Controls.Remove ("lblNew1")
MsgBox "Image Control Deleted"
End Sub

When we click on Delete Command Button:

You might also like