Learn how to create interactive Excel forms using CheckBox and Option Button controls in 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.
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.
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
This enhanced example shows how checkboxes can be used to calculate total prices based on selected options.
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 ...
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.
| 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 |
This example demonstrates how to use option buttons to create a simple survey question where users can select only one answer.
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
This example shows how option buttons can be used to select shipping methods with different costs and delivery times.
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
Copyright ® 2008- Dr.Liew Voon Kiong . All rights reserved [Privacy Policy]
Contact: Facebook Page | Email