Exiting listboxes in a userform

N

nir020

I have created a userform in Excel which contains a listbox to which the user
can make multi selections.

Is it possible to include some code that will when the user exits the form
will only show the elements of the userform that have been selected and will
hide the elements that have not been selected.

Thanks
 
R

Rick Rothstein

You didn't say where you wanted to "show" the selected items at when the
UserForm is closed. Here is code that puts the selected items into an array
in code... you can use that to see how to read the selected items (the
For..Next loop is the key to that) and then put them wherever it is you
wanted them to go...

Dim X As Long
Dim Index As Long
Dim SelectedItems() As String
With Me.ListBox1
ReDim SelectedItems(0 To .ListCount)
For X = 0 To .ListCount - 1
If .Selected(X) Then
SelectedItems(Index) = .List(X)
Index = Index + 1
End If
Next
ReDim Preserve SelectedItems(0 To Index - 1)
End With
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top