Got any Excel/VBA Questions? Free Excel Help
When using a ListBox we can do so in a way that can allow users to make multiple selections from the ListBox. After they have made their selection(s) normally a CommandButton would be used to take those selections and place them onto a Worksheet or another UserForm Control.
Single Selection ListBox
As mentioned above, we can have our user select one, or more items in a ListBox. The procedure below take a single selected ListBox item and places it into a range at the bottom of a list.
Sheet2.Range("A65536").End(xlUp)(2, 1) = ListBox1.Value
For such code to work the ListBox must have its MultiSelect Property set to 1 fmMultiSelectSingle
MutliSelect Property is -1 fmMultiSelectMulti
When have set the MultiSelect Property set to -1 fmMultiSelectMulti we can allow users to make multiple selections. However, due to MultiSelect Property set to -1 fmMultiSelectMulti we can no longer use simple code like above to return the selected items to our cells. To do this with multiple selections we can use code like shown below;
Private Sub CommandButton2_Click() Dim lItem As Long For lItem = 0 To ListBox1.ListCount - 1 If ListBox1.Selected(lItem) = True Then Sheet2.Range("A65536").End(xlUp)(2, 1) = ListBox1.List(lItem) ListBox1.Selected(lItem) = False End If Next End Sub
Minimize/Maximize Button to a UserForm |
Excel VBA Macro Codes Tips & Tricks |
Excel Message Box (MsgBox) Function |
See also Index to Excel VBA Code and Index to Excel Freebies and Lesson 1 - Excel Fundamentals and Index to how to… providing a range of solutions.
Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.