What is multi select list box?

In this tutorial we will be looking at multi select listboxes. This is an Excel VBA tutorial for Windows operating systems.. This fantastic multi select listbox will enable you to select multiple rows in your listbox and send all or some of the row values to any destination in your file. You can select just one row or multiple rows then when you click the command button and the values will be sent to the destination sheet.

The completed file is available for download below.

Did you set the selectionmode to multi?

You need to specify that in order to allow multiple selections.

Then you can do:

Dim i as Integer=0 For i=0 To Me.listBox.SelectedItems.Count -1 'display the listbox value next i

Here is a screen shot:

After you set the property on the listbox then call setselected based on the values you want selected.

me.lstItemSizes.SetSelected[3,true] me.lstItemSizes.SetSelected[4,true] me.lstItemSizes.SetSelected[9,true]

Here you can add 20 numbers and only select the even.

Dim i As Integer 'load the list with 20 numbers For i = 0 To 20 Me.ListBox1.Items.Add[i] Next 'now use setselected 'assume only even are selected For i = 0 To 20 If i Mod 2 = 0 Then Me.ListBox1.SetSelected[i, True] End If Next

Look at the way you are looping, lets assume I create a list of integers, my vb.net is rusty I mainly develop in C#. But assume you did this:

Dim l As New List[Of Integer] l.Add[2] l.Add[6] l.Add[20]

You only have three items in your list, so first loop based on the items on your list, then within the items in your listbox, you have it vice versa. Look at this:

Dim i As Integer Dim l As New List[Of Integer] l.Add[2] l.Add[6] l.Add[20] 'load the list with 20 numbers For i = 0 To 20 Me.ListBox1.Items.Add[i] Next Dim lCount As Integer = 0 For lCount = 0 To l.Count - 1 For i = 0 To 20 If i = l.Item[lCount] Then Me.ListBox1.SetSelected[i, True] Exit For End If Next Next

In the code my l is a list of just 3 items: 2, 6, and 20. I add these items to l which is just a list object. So now I have to loop using these 3 numbers and compare with my listbox. You have it the opposite you are looping on your listbox and then taking into account the list object.

Notice in my for loop that once the item in my list is found I no longer need to loop so I exit for. This ensures I dont overdue the amount of looping required. Once the item is found get out and go back to the count of your list object count.

After running my code here is the result

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.

Video liên quan

Chủ Đề