Lesson 22: The CheckBox and the Option Button
Continue learning classic Excel VBA with the same shared lesson template and cleaner visual style.
Learn how to create interactive Excel forms using CheckBox and Option Button controls in VBA.
22.1 The CheckBox Control in Excel VBA
The CheckBox is a fundamental control in Excel VBA that allows users to make multiple selections from a set of options. Unlike option buttons (radio buttons), checkboxes operate independently, meaning users can select none, one, or multiple checkboxes simultaneously.
Key Properties of CheckBox Controls:
- Value: True if checked, False if unchecked (most important property)
- Caption: The text displayed next to the checkbox
- LinkedCell: The worksheet cell that displays the checkbox value
- Enabled: Determines if the checkbox can be interacted with
- Visible: Controls whether the checkbox is shown or hidden
Practical Applications of CheckBoxes:
- Creating preference settings in user forms
- Building interactive checklists or to-do lists
- Developing survey forms with multiple-choice questions
- Creating shopping carts or order forms
- Implementing filter options for data displays
Example 22.1: Fruit Sales Tracker
This example demonstrates how to use checkboxes to display different sales data. Users can select to view apple sales, orange sales, or the total combined sales.
VBA Code:
Private Sub CommandButton1_Click()
If CheckBox1.Value = True And CheckBox2.Value = False Then
MsgBox "Quantity of apple sold is " & Cells(2, 2).Value
ElseIf CheckBox2.Value = True And CheckBox1.Value = False Then
MsgBox "Quantity of orange sold is " & Cells(2, 3).Value
Else
MsgBox "Total quantity of fruits sold is " & Cells(2, 4).Value
End If
End Sub
Implementation Steps:
- Insert two CheckBox controls from the Developer tab
- Set their Caption properties to "Apple Sales" and "Orange Sales"
- Add a CommandButton to execute the code
- Enter sales data in cells B2 (apples), C2 (oranges), and D2 (total)
- Paste the above code in the CommandButton's click event
Advanced CheckBox Example: Dynamic Pricing Calculator
This enhanced example shows how checkboxes can be used to calculate total prices based on selected options.
VBA Code:
Private Sub CalculateTotal()
Dim total As Currency
total = 0
If CheckBox1.Value Then total = total + 25.99 ' Basic package
If CheckBox2.Value Then total = total + 15.50 ' Add-on A
If CheckBox3.Value Then total = total + 9.99 ' Add-on B
If CheckBox4.Value Then total = total + 19.99 ' Premium support
' Apply discount if all options selected
If CheckBox1.Value And CheckBox2.Value And _
CheckBox3.Value And CheckBox4.Value Then
total = total * 0.9 ' 10% discount
End If
LabelTotal.Caption = "Total: $" & Format(total, "0.00")
End Sub
Private Sub CheckBox1_Click()
CalculateTotal
End Sub
Private Sub CheckBox2_Click()
CalculateTotal
End Sub
' ... similar click events for other checkboxes ...
22.2 The Option Button Control
Option buttons (also called radio buttons) allow users to select exactly one choice from a set of mutually exclusive options. When one option button is selected, all others in the same group are automatically deselected.
Key Differences Between CheckBoxes and Option Buttons:
| Feature | CheckBox | Option Button |
|---|---|---|
| Selection | Multiple items can be selected | Only one item can be selected |
| Group behavior | Independent | Mutually exclusive within group |
| Typical use | Selecting multiple options | Choosing one option from many |
Example 22.2: Survey Question with Option Buttons
This example demonstrates how to use option buttons to create a simple survey question where users can select only one answer.
VBA Code:
Private Sub CommandButton1_Click()
If OptionButton1.Value = True Then
MsgBox "You selected: Option 1", vbInformation, "Survey Response"
ElseIf OptionButton2.Value = True Then
MsgBox "You selected: Option 2", vbInformation, "Survey Response"
ElseIf OptionButton3.Value = True Then
MsgBox "You selected: Option 3", vbInformation, "Survey Response"
Else
MsgBox "Please select an option", vbExclamation, "Selection Required"
End If
End Sub
Implementation Tips:
- Group related option buttons by placing them in the same Frame control
- Set meaningful Caption properties for each option button
- Consider setting a default selection (Value = True) for one option
- Always validate that at least one option is selected before processing
Advanced Option Button Example: Shipping Method Selector
This example shows how option buttons can be used to select shipping methods with different costs and delivery times.
VBA Code:
Private Sub UpdateShippingInfo()
Dim shippingMethod As String
Dim cost As Currency
Dim deliveryDays As Integer
If optStandard.Value Then
shippingMethod = "Standard Shipping"
cost = 5.99
deliveryDays = 5
ElseIf optExpress.Value Then
shippingMethod = "Express Shipping"
cost = 12.99
deliveryDays = 2
ElseIf optOvernight.Value Then
shippingMethod = "Overnight Shipping"
cost = 24.99
deliveryDays = 1
End If
lblMethod.Caption = "Method: " & shippingMethod
lblCost.Caption = "Cost: $" & Format(cost, "0.00")
lblDelivery.Caption = "Delivery: " & deliveryDays & " business day(s)"
End Sub
Private Sub optStandard_Click()
UpdateShippingInfo
End Sub
Private Sub optExpress_Click()
UpdateShippingInfo
End Sub
Private Sub optOvernight_Click()
UpdateShippingInfo
End Sub
Best Practices for CheckBoxes and Option Buttons
Summary: Key Takeaways
🔗 Related Resources