Exiting listboxes in a userform

  • Thread starter Thread starter nir020
  • Start date Start date
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
 
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
 
Back
Top