Consulting

Results 1 to 5 of 5

Thread: Multiselect listbox values to a textbox

  1. #1

    Multiselect listbox values to a textbox

    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

    [VBA]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[/VBA]

    Cheers

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    or:
    [VBA]
    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
    [/VBA]

  4. #4

    thanks

    Thanks Kenneth and snb
    Sorry both lost me, kenneth im working on what you gave me thanks

  5. #5
    Thanks so much I got it, by playing with snb's code a bit.
    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •