Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get values from a listbox on a sheet

Tags:

excel

vba

listbox

I have a listbox named ListBox1 on Sheet1 of an Excel workbook.

Every time the user selects one of the items in the list, I need to copy its name to a variable named strLB.

So, if I have Value1, Value2, Value3, Value4 and the user selects Value1 and Value3, I need my strLB to come out as Value1,Value3.

I tried doing that post hoc with:

For i = 1 To ActiveSheet.ListBoxes("ListBox1").ListCount
    If ActiveSheet.ListBoxes("ListBox1").Selected(i) Then strLB = strLB & etc.etc.
Next i

But this is very slow (I have 15k values in my listbox). This is why I need to record the selection in real time and not in a cycle, after the user is done inputting.

I'm going to also need a way to check if the user removed any of the previous selection.

like image 638
Bruder Avatar asked Mar 22 '12 12:03

Bruder


People also ask

How can you retrieve a list of selected items from a multiselect ListBox control?

To do this, press ALT+F11. If the Properties dialog box is not visible, click Properties on the View menu. Project Explorer on the View menu.

How do you retrieve the selected item in a ListBox?

To retrieve a collection containing all selected items in a multiple-selection ListBox, use the SelectedItems property. If you want to obtain the index position of the currently selected item in the ListBox, use the SelectedIndex property.

Which function is used to fetch a value based on the number returned by a ListBox control?

ListBox control has a GetItemText which helps you to get the item text regardless of the type of object you added as item. It really needs such GetItemValue method. Using above method you don't need to worry about settings of ListBox and it will return expected Value for an item.

What is ListBox value?

The list box in Excel VBA is a list assigned to a variable. This list has various inputs to select from and allows selecting multiple options at once. A list box can be inserted on a UserForm by choosing the list box option. List boxes use named ranges having certain values.


2 Answers

Unfortunately for MSForms list box looping through the list items and checking their Selected property is the only way. However, here is an alternative. I am storing/removing the selected item in a variable, you can do this in some remote cell and keep track of it :)

Dim StrSelection As String

Private Sub ListBox1_Change()
    If ListBox1.Selected(ListBox1.ListIndex) Then
        If StrSelection = "" Then
            StrSelection = ListBox1.List(ListBox1.ListIndex)
        Else
            StrSelection = StrSelection & "," & ListBox1.List(ListBox1.ListIndex)
        End If
    Else
        StrSelection = Replace(StrSelection, "," & ListBox1.List(ListBox1.ListIndex), "")
    End If
End Sub
like image 91
Siddharth Rout Avatar answered Sep 22 '22 04:09

Siddharth Rout


The accepted answer doesn't cut it because if a user de-selects a row the list is not updated accordingly.

Here is what I suggest instead:

Private Sub CommandButton2_Click()
    Dim lItem As Long

    For lItem = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(lItem) = True Then
            MsgBox(ListBox1.List(lItem))
        End If
    Next
End Sub

Courtesy of http://www.ozgrid.com/VBA/multi-select-listbox.htm

like image 24
Joan-Diego Rodriguez Avatar answered Sep 22 '22 04:09

Joan-Diego Rodriguez