The MultiSelect property in Excel VBA allows a user to select multiple items in a list box. The Userform we are going to create looks as follows:
To create this Userform, execute the following steps.
1. Open the Visual Basic Editor. If the Project Explorer is not visible, click View, Project Explorer.
2. Click Insert, Userform. If the Toolbox does not appear automatically, click View, Toolbox. Your screen should be set up as below.
3. Add the list boxes (first at the left, the second at the right), command buttons, check boxes (first at the left, the second at the right), frame and option buttons (first at the top, the second below the first, and so on). Once this has been completed, the result should be consistent with the picture of the Userform shown earlier. For example, create a list box control by clicking on ListBox from the Toolbox. Next, you can drag a list box on the Userform. When you arrive at the ‘Select Type’ frame, remember to draw this frame first before you place the three option buttons in it.
4. You can change the names and the captions of the controls. Names are used in the Excel VBA code. Captions are those that appear on your screen. It is good practice to change the names of the controls, but it is not necessary here because we only have a few controls in this example. To change the caption of the Userform, command buttons, check boxes, frame and option buttons, click View, Properties Window and click on each control.
5. To show the Userform, place a command button on your worksheet and add the following code line:
Private Sub CommandButton1_Click() UserForm1.Show End Sub
We are now going to create the Sub UserForm_Initialize. When you use the Show method for the Userform, this sub will automatically be executed.
6. Open the Visual Basic Editor.
7. In the Project Explorer, right click on UserForm1 and then click View Code.
8. First, declare the variable i of type Integer. Declare the variable in the General Declarations section (at the top of the code). This way you only have to declare the variable once and you can use them in multiple subs.
Dim i As Integer
9. Choose Userform from the left drop-down list. Choose Initialize from the right drop-down list.
10. Add the following code lines:
Private Sub UserForm_Initialize() With ListBox1 .AddItem "Sales" .AddItem "Production" .AddItem "Logistics" .AddItem "Human Resources" End With OptionButton3.Value = True End Sub
Explanation: the first list box will be filled and the third option button is set as default.
We have now created the first part of the Userform. Although it looks neat already, nothing will happen yet when we click the command buttons or the other controls.
11. In the Project Explorer, double click on UserForm1.
12. Double click on the Add button.
13. Add the following code lines:
Private Sub CommandButton1_Click() For i = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(i) = True Then ListBox2.AddItem ListBox1.List(i) Next i End Sub
Explanation: Excel VBA loops through the first list box (list index number of zero (0) for the first item in the list) and, if selected, adds the item to the second list box.
14. Double click on the Remove button.
15. Add the following code lines:
Private Sub CommandButton2_Click() Dim counter As Integer counter = 0 For i = 0 To ListBox2.ListCount - 1 If ListBox2.Selected(i - counter) Then ListBox2.RemoveItem (i - counter) counter = counter + 1 End If Next i CheckBox2.Value = False End Sub
Explanation: Excel VBA loops through the second list box and, if selected, removes the item. The counter variable holds track of the number of removed items.
16. Double click on the first option button.
17. Add the following code lines:
Private Sub OptionButton1_Click() ListBox1.MultiSelect = 0 ListBox2.MultiSelect = 0 End Sub
18. Double click on the second option button.
19. Add the following code lines:
Private Sub OptionButton2_Click() ListBox1.MultiSelect = 1 ListBox2.MultiSelect = 1 End Sub
20. Double click on the third option button.
21. Add the following code lines:
Private Sub OptionButton3_Click() ListBox1.MultiSelect = 2 ListBox2.MultiSelect = 2 End Sub
Explanation: the ‘Select Type’ setting can be chosen by clicking on the option buttons. The picture of the Userform shown earlier gives a description of each setting. Instead of configuring this setting at runtime, you can also configure this setting at design time. To achieve this, right mouse click on a list box control, and then click on Properties. Set the MultiSelect property to 0 - fmMultiSelectSingle, 1 - fmMultiSelectMulti or 2 - fmMultiSelectExtented.
22. Double click on the first check box.
23. Add the following code lines:
Private Sub CheckBox1_Click() If CheckBox1.Value = True Then For i = 0 To ListBox1.ListCount - 1 ListBox1.Selected(i) = True Next i End If If CheckBox1.Value = False Then For i = 0 To ListBox1.ListCount - 1 ListBox1.Selected(i) = False Next i End If End Sub
Explanation: by checking the first check box, all the items of the first list box can be selected / deselected.
24. Double click on the second check box to add the same code lines. Only replace CheckBox1 with CheckBox2 and ListBox1 with ListBox2.