VBA Listbox multiselect question

nuggett

Board Regular
Joined
Apr 26, 2006
Messages
78
Hi

Can anyone help with code that will capture the selected items in a multiselect listbox and store them as variables that can be used in other modules. Best I have come up with so far is to capture the values and send them to a nominated range. I can then refer to those cells and give each variable the value of that cell but I am sure there must be a cleaner way....I just don't know what it is?
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi

I have got some code which does what you are looking to do but I am a newbie and it is probably not the cleanest way. It would be great if anyone has any clue how to achieve the results this code achieves using some sort of array.

Private Sub EnterBtn_Click()
If ListBox1.Selected(0) = True Then
msg1 = ListBox1.List(0)
End If
If ListBox1.Selected(1) = True Then
msg2 = ListBox1.List(1)
End If
If ListBox1.Selected(2) = True Then
msg3 = ListBox1.List(2)
End If
If ListBox1.Selected(3) = True Then
msg4 = ListBox1.List(3)
End If
If ListBox1.Selected(4) = True Then
msg5 = ListBox1.List(4)
End If

MsgBox msg1 & " " & msg2 & " " & msg3 & " " & msg4 & " " & msg5
End Sub
 
Upvote 0
This function returns a string showing the selected items from the list box.
If ListBoxSummary(ListBox1) = "0011" that means that there are 4 items in the list box and the third and fourth have been selected.

Put this in a normal code module
Code:
Sub test()
    UserForm1.Show
    MsgBox "UF unloaded. ListBox 1 was " & ListBoxSummary()
End Sub

Function ListBoxSummary(Optional inputBox As Object) As String
    Static myValue As String
    Dim i As Long
    
    If Not inputBox Is Nothing Then
        myValue = vbNullString
        With inputBox
            For i = 0 To .ListCount - 1
                myValue = CStr(-CLng(.Selected(i))) & myValue
            Next i
        End With
    End If
    
    ListBoxSummary = myValue
End Function

If this is put in the userform's code module
Code:
Private Sub UserForm_Terminate()
    If ListBoxSummary(Me.ListBox1) = "cat" Then Exit Sub
End Sub
Even after the uf has terminated, the function ListBoxSummary will return the value from the (now unloaded) userform, as in the sub Test.

When called from the userform code, the Listbox in question should be passed as the function's argument. This forces an update of the value of the function.
When called from outside the UF code, omitting the argument causes the function not read from the userform, but to "remember" the static variable myValue.
 
Last edited:
Upvote 0
Also for use after the UF is closed.
Code:
Function PostSelected(index As Long) As Boolean
    PostSelected = CBool(Val("&O" & ListBoxSummary) And (8 ^ index))
End Function
It reproduced the .Selected property by polling the memory value of ListBoxSummary.

Its 0 based if ListBoxSummary = "1011" then

PostSelected(0)=True
PostSelected(1)=True
PostSelected(2)=False
PostSelected(3)=True
 
Upvote 0

Forum statistics

Threads
1,217,973
Messages
6,139,704
Members
450,226
Latest member
DrKraGeN

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top