PDA

View Full Version : Multiselect listbox values to a textbox



peterwmartin
03-27-2013, 01:56 PM
Hi all,
need help with writing multiselected values in a listbox to a userform text box. Have multi select working however it is only writing 1 value

eg
name1
name2
name3
name4

to a textbox as name1, name3, name4

this is a variation of what I had, however this only puts the first name on the list in the text box.
A previous version put the first selected name now im lost

Private Sub CommandButton1_Click()
Dim Item As Long
Item = 1
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) = True Then
UserForm1.TextBox4 = ListBox1.List(lItem)
ListBox1.Selected(Item) = False
End If
Next i
End Sub

Cheers

Kenneth Hobs
03-27-2013, 02:29 PM
Build a string. Put the contents of the textbox into a string and then do your loop. IF the string is <> "" for the first loop then concatenate vblf and then concatenate the listbox item selected in the loop. After looping, add the string to the textbox.

snb
03-27-2013, 03:33 PM
or:

Private Sub Commancbutton1_Click()
sn = lijst.List


For j = 0 To UBound(lijst.List) - 1
If Not lijst.Selected(j) Then sn(j + 1, 0) = "~"
Next


TextBox1.Text = Join(Filter(Application.Transpose(Application.Index(sn, , 1)), "~", False), "|")
End Sub

peterwmartin
03-27-2013, 04:26 PM
Thanks Kenneth and snb
Sorry both lost me, kenneth im working on what you gave me thanks

peterwmartin
03-27-2013, 04:56 PM
Thanks so much I got it, by playing with snb's code a bit.
Cheers