Excel UserForm Controls - CheckBox, OptionButton and ToggleButton
Excel UserForm Controls - CheckBox, OptionButton and ToggleButton
Excel VBA
User Rating: / 24
Poor Best
Details
Also refer "2. UserForm and Controls - Properties" for properties common to the UserForm and
most Controls
Note: In below given examples, vba codes are required to be entered in the Code Module of the
UserForm, unless specified otherwise.
___________________________________________________________________________
1. UserForms in Excel VBA - UserForm Basics; Add Controls dynamically at run-time using the
Add Method; UserForm Controls (A Snapshot).
2. UserForm and Controls - Properties.
3. UserForm Controls - Label, TextBox and CommandButton.
4. UserForm Controls - ComboBox and ListBox.
5. UserForm Controls - CheckBox, OptionButton and ToggleButton
6. UserForm Controls - Frame, MultiPage and TabStrip.
7. UserForm Controls - ScrollBar and SpinButton.
8. UserForm Controls - Image and RefEdit.
___________________________________________________________________________
CheckBox
The Value property of a CheckBox indicates whether it is selected or not. A True value indicates that the
CheckBox is selected, False indicates that it is cleared; and the Null value indicates that it is neither
selected nor cleared, and the CheckBox will appear shaded in this case. The value of TripleState property
should be set (can be set either in the Properties window or by using a macro or vba code) to True for the
CheckBox to have a Null value.
With Me.CheckBox1
.TextAlign = fmTextAlignCenter
'will enable CheckBox to have three possible values, including the Null value
.TripleState = True
End With
End Sub
End Sub
Me.TextBox1.Enabled = False
End Sub
End Sub
---------------------------------------------------------------------------------------------------------------------------------
OptionButton
OptionButton is used to make one selection from multiple options. It is also referred to as a Radio Button,
which chooses one option from a group of mutually exclusive options. If OptionButtons are not grouped,
selecting one OptionButton in a UserForm will de-select all other OptionButtons in the form. All
OptionsButtons within a specific Group become mutually exclusive and self-contained within that group
and do not affect selection of OptionButtons outside that group. Selecting an OptionButton in one group
will de-select all other OptionButtons of only that group.
OptionButtons can be grouped by the following methods: (i) using the GroupName property:
OptionButtons can first be added to a UserForm and then their GroupName property can be set either in
the Properties window or by using a macro or vba code. OptionButtons having the same GroupName
become mutually exclusive and selecting one OptionButton will de-select all other OptionButtons of that
group; and (ii) using a Frame Control: First add the Frame in a UserForm and then add OptionButtons to
the Frame. A Frame can contain other Controls as well. Each Frame will separately group two or more
OptionButtons, irrespective of whether all OptionButtons across other Frames, also have the same
GroupName. However, it might be preferable to group OptionButtons using the GroupName property
than using a Frame only for this purpose, to save space and reduce the UserForm size. Note: A MultiPage
control is also a container and is used to group or organize related controls within each of its pages. Each
page of a MultiPage control will separately group two or more OptionButtons, irrespective of whether all
OptionButtons (across all pages) have the same GroupName.
The Value property of an OptionButton indicates whether it is selected or not. A True value indicates that
the OptionButton is selected, False is the default value and indicates that it is not selected.
Example 3: Determine which controls (CheckBox & OptionButton) within a Frame, are selected:
End Sub
End Sub
selected.
'The test for CheckBoxes (viz. If TypeOf ctrl Is msforms.CheckBox Then) might not be conclusive
- the OptionButton and ToggleButton controls also implement the Checkbox interface and meet
this condition.
For Each ctrl In Frame1.Controls
If TypeOf ctrl Is msforms.CheckBox Then
If Not TypeOf ctrl Is msforms.OptionButton Then
If ctrl.Value = True Then
MsgBox ctrl.Caption & " is selected", vbOKOnly, "Selected CheckBoxes"
End If
End If
End If
Next ctrl
End Sub
End Sub
With CheckBox, user can select multiple options, by selecting each CheckBox against which an option
is available. OptionButton is used when user is allowed to select only a single option.
CheckBox can be used in a singular manner viz. a single CheckBox can be selected or deselected (by
clicking itself) indicating whether an option is to be exercised or not. OptionButton on the other hand
are used in multiples viz. two or more, to indicate selection of mutually exculsive options.
OptionButton gets deselected only on selection of another OptionButton and not by clicking itself.
Image 23a
Step 1: refer Image 23a - clicking on Command Button (Get UserForm) runs the following macro in
Sheet4, which Loads the UserForm:
Sub getUserForm()
frmGetData.Show
End Sub
Image 23b
Image 23c
Step 3: refer Image 23c - enter UserForm fields for each candidate.
Image 23d
Step 4: refer Image 23d - save UserForm and data gets posted in worksheet, sorted alphabetically, by
name.
Set of VBA codes/macros entered in the Code Module of the UserForm: VBE editor -> Forms ->
frmGetData (name of the UserForm):
Dim i As Integer
Dim myArray As Variant
'to select from a large number of options, use a ListBox or ComboBox; CheckBoxes might
provide better visibility and can be used when selection is to be made from few options, hence
these are used to select user "Interests".
With Me.lstAge
For i = 1 To 100
.AddItem i & " yrs"
Next i
End With
'ComboBox has been preferred over ListBox, for selecting country, for 2 reasons: (i) allow
customized entries from user even if country name not in list; and (ii) to facilitate selection from
list because typing in the first letter(s) in text area of ComboBox will display matching options.
myArray = Array("Argentina", "Australia", "Brazil", "China", "France", "Germany", "Greece",
"HongKong", "India", "Italy", "Japan", "Russia", "Singapore", "Spain", "Switzerland", "UK",
"USA")
Me.cmbCountry.List = myArray
Me.txtSpouse.Enabled = False
Me.txtStartDate.Enabled = False
Me.txtEndDate.Enabled = False
'all OptionsButtons within a specific GroupName become mutually exclusive and self-contained
within that group and do not affect selection of OptionButtons outside that group.
'selecting an OptionButton in one group will de-select all other OptionButtons of only that group.
'if OptionButtons are not grouped (in a container viz. specific GroupName, MultiPage Control or
Frame Control), selecting one OptionButton in a UserForm will de-select all other OptionButtons
in the form.
optMale.GroupName = "Gender"
optFemale.GroupName = "Gender"
optStartDate.GroupName = "Availability"
optEndDate.GroupName = "Availability"
End Sub
End Sub
'manual entry has been disabled in the TextBoxes txtStartDate and txtEndDate. Select relevant
OptionButton and click on calendar to post.
If optStartDate = True Then
txtStartDate.Text = Format(Calendar1.Value, "mm/dd/yyyy")
ElseIf optEndDate = True Then
txtEndDate.Text = Format(Calendar1.Value, "mm/dd/yyyy")
End If
End Sub
End Sub
clearForm
End Sub
Unload Me
End Sub
2)
If endDate < startDate Then
MsgBox "Start Date cannot be later than End Date!", vbOKOnly, "Date Error!"
Exit Sub
End If
Sheet4.Cells(totalRows + 1, 3) = lstAge.Value
Sheet4.Cells(totalRows + 1, 5) = cmbCountry.Value
Sheet4.Cells(totalRows + 1, 7) = txtStartDate.Value
Sheet4.Cells(totalRows + 1, 8) = txtEndDate.Value
'run the macro - Private Sub clearForm(), to clear all fields in UserForm
clearForm
End Sub
---------------------------------------------------------------------------------------------------------------------------------
ToggleButton
ToggleButton is a control which executes one action when clicked first and a different action on the
second click. It has two states, On and Off, wherein the button alternates (or toggles) between the two. It
can have a value of True (where it appears as pressed) when the button is selected or the value False
(appears unpressed) when unselected. ToggleButton seems a cross between a CheckBox (toggle
functionality) and a CommandButton (clickable and similar appearance till clicked). You can toggle On and
Off by selecting or deselecting a CheckBox. ToggleButton uses a single button whereas OptionButtons use
two separate buttons to determine an on/off (select/deselect) status. However, most users find CheckBox
or OptionButtons easier to use. Note: ToggleButton can also have a Null value (ie. neither selected nor
unselected) wherein it will appear shaded, if the TripleState property is set to True.
Example 8: Using a ToggleButton control to alternate between sorting as per Ascending order
or Descending order of a worksheet range. Refer Images 24a and 24b:
Image 24a
Image 24b
'ToggleButton is pressed, data in column A & B gets sorted in ascending order and the button
appears in green BackColor and displays the alternate sort option:
Me.ToggleButton1.Value = True
Me.ToggleButton1.Caption = "Get Descending Order"
Me.ToggleButton1.Font.Bold = True
Me.ToggleButton1.BackColor = RGB(0, 255, 0)
End Sub
'if ToggleButton is pressed, data in column A & B gets sorted in ascending order and the button
appears in green BackColor and displays the alternate sort option:
If Me.ToggleButton1.Value = True Then
Me.ToggleButton1.Caption = "Get Descending Order"
Me.ToggleButton1.BackColor = RGB(0, 255, 0)
Sheet7.Range("A2:B" & totalrows).Sort Key1:=Sheet7.Range("A2"), Order1:=xlAscending
'if ToggleButton value is False (appears unpressed), data in column A & B gets sorted in
descending order and the button appears in red BackColor and displays the alternate sort option:
ElseIf Me.ToggleButton1.Value = False Then
Me.ToggleButton1.Caption = "Get Ascending Order"
Me.ToggleButton1.BackColor = RGB(255, 0, 0)
Sheet7.Range("A2:B" & totalrows).Sort Key1:=Sheet7.Range("A2"), Order1:=xlDescending
End If
End Sub
End Sub
End Sub
Note: A CheckBox can also be used to Toggle, just like the ToggleButton above.
End Sub
End Sub