Excel VBA Lesson 22: Working with Checkbox and Option Button

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on Reddit

<<Lesson 21>> [Contents] <<Lesson 23>>

There are many Excel VBA controls that can be used to perform certain tasks by writing Excel VBA code for them. These controls are also known as Active-X controls. These controls are Excel VBA objects so they have their own properties, methods and events. They can be found on the Excel Control Toolbox, as shown in theFigure 17.1. We shall deal with the checkbox, the textbox and the option button.

vba_Figure18.1

Figure 22.1

22.1 The CheckBox

The Checkbox is a very useful control in Excel VBA. It allows the user to select one or more items by checking the check box or check boxes concerned. For example, you may create a shopping cart where the user can click on check boxes that correspond to the items they intend to buy, and the total payment can be computed at the same time.One of the most important properties of the checkbox is Value. If the check box is selected or checked, the value is true, whilst if it is not selected or unchecked, the Value is False.
The usage of the checkbox is illustrated in Example 22.1



Example 22.1

In this example, the user can choose to display the sale volume of one type of fruits sold or total sale volume. The code is shown below:

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 “Quantity of Fruits sold is” & Cells (2, 4).Value
End If

End Sub




The Interface is shown in Figure 22.2

vba_Figure18.2

Figure 22.2

22.2 The TextBox

The TextBox is the standard Excel VBA control for accepting input from the user as well as to display the output. It can handle string (text) and numeric data but not images.

Example 22.2

In this example, we inserted two text boxes and display the sum of numbers entered into the two text boxes in a message box. The Val function is used to convert the string into numeric values because the text box treats the number entered as a string.

Private Sub CommandButton1_Click ()
Dim x As Variant, y As Variant, z As Variant
x = TextBox1.Text
y = TextBox2.Text
z = Val(x) + Val(y)
MsgBox “The Sum of ” & x & ” and ” & y & ” is ” & z
End Sub

vba_Figure18.3

Figure 17.3

22.3 The Option Button

The option button control also lets the user selects one of the choices. However, two or more option buttons must work together because as one of the option buttons is selected, the other option button will be deselected. In fact, only one option button can be selected at one time. When an option button is selected, its value is set to “True” and when it is deselected; its value is set to “False”.

Example 22.3

This example demonstrates the usage of the option buttons. In this example, the Message box will display the option button selected by the user. The output interface is shown in Figure 22.4.

The code

Private Sub OptionButton1_Click ()

MsgBox “Option 1 is selected”
End Sub
Private Sub OptionButton2_Click()
MsgBox “Option 2 is selected”
End Sub
Private Sub OptionButton3_Click()
MsgBox “Option 3 is selected”

End Sub

The Output Interface

vba_Figure18.4

Figure 22.4


<<Lesson 21>> [Contents] <<Lesson 23>>

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on Reddit