We have learned how to work with check boxes, option buttons, and text boxes in Excel 2010 VBA in the previous lessons. We shall continue to learn how to manipulate other controls in Excel VBA 2010 in this lesson. In this lesson, we will deal with List Box, Combo Box, and Toggle Button.
Private Sub CommandButton1_Click() For x = 1 To 10 ListBox1.AddItem “Apple” Next End Sub Private Sub CommandButton2_Click() For x = 1 To 10 ListBox1.Clear Next End Sub
23.2 Combo Box
The function of the Combo Box is also to present a list of items where the user can click and select the items from the list. However, the user needs to click on the small arrowhead on the right of the combo box to see the items which are presented in a drop-down list. In order to add items to the list, you can also use the AddItem method.
23.3 Toggle Button
Toggle button lets the user switches from one action to another alternatively. When the Toggle button is being depressed, the value is true and when it is not depressed, the value is false. By using the If and Else code structure, we can thus switch from one action to another by pressing the toggle button repeatedly.
In this example, the user can toggle between apple and orange as well as font colors.
Private Sub ToggleButton1_Click () If ToggleButton1.Value = True Then Cells (1, 1) = “Apple” Cells (1, 1).Font.Color = vbRed Else Cells (1, 1) = “Orange” Cells (1, 1).Font.Color = vbBlue End If End Sub
View the animated image in Figure 23.3