+ Reply to Thread
Results 1 to 21 of 21

Listbox to String

Hybrid View

  1. #1
    Forum Contributor FRIEL's Avatar
    Join Date
    03-07-2008
    Location
    Coventry
    MS-Off Ver
    2003 and 2007
    Posts
    266

    Listbox to String

    Hi all

    I have a Userform with a listbox called listbox2
    I want the entire contents of this list box to be in a string in Variable Recs

    I have this
    Dim recs As String
    If ListBox2.ListCount >= 1 Then
    recs = ""
    For x = 0 To ListBox2.ListCount - 1
    recs = recs & ListBox2.List(x) & " ; "
    Next x
    
    End If
    And it works
    but I believe there is an easier way to do it
    I think using the list properity
    I dont like to use code i dont understand
    it makes it hard to use in other situations
    so please try to be as clear and patent as possible with me

    Criticism is welcomed

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Listbox to String

    sub snb()
      recs=Join(listbox2.list,";")
    End sub



  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Listbox to String

    I don't think that you can avoid the loop, how is the listbox populated?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,317

    Re: Listbox to String

    If you populated using List or Rowsource (assuming a one column range) then you'll need to transpose before you join; if you used additem, then you need to loop.
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Listbox to String

    Sub snb()
      ListBox1.List = Split("aa|bb|cc|dd", "|")
      sn = Join(Application.Transpose(ListBox1.List), "|")
        
      ListBox1.List = Array("aa", "bb", "cc", "dd")
      sn = Join(Application.Transpose(ListBox1.List), "|")
        
      ListBox1.Clear
      ListBox1.AddItem "aa"
      ListBox1.AddItem "bb"
      sn = Join(Application.Transpose(ListBox1.List), "|")
    
      ListBox1.ListFillRange = "A1:A5"
      sn = Join(Application.Transpose(ListBox1.List), "|")
    
      ListBox1.ListFillRange = ""
      ListBox1.List = Range("A1:A5").Value
      sn = Join(Application.Transpose(ListBox1.List), "|")
    
      ListBox1.List = Range("A1:C5").Value
      sn = Join(Application.Transpose(Application.Index(ListBox1.List, , 1)), "|")
    
      ListBox1.RowSource = "A1:A5"
      sn = Join(Application.Transpose(ListBox1.List), "|")
    
    End Sub
    Last edited by snb; 07-07-2011 at 05:29 AM.

  6. #6
    Forum Contributor FRIEL's Avatar
    Join Date
    03-07-2008
    Location
    Coventry
    MS-Off Ver
    2003 and 2007
    Posts
    266

    Re: Listbox to String

      ListBox1.List = ThisWorkbook.Sheets("EMAIL").Range("A2:A36").Value
    which method is that?

    How do you transpose?

  7. #7
    Forum Contributor FRIEL's Avatar
    Join Date
    03-07-2008
    Location
    Coventry
    MS-Off Ver
    2003 and 2007
    Posts
    266

    Re: Listbox to String

    [QUOTE=FRIEL;2558478]
      ListBox1.List = ThisWorkbook.Sheets("EMAIL").Range("A2:A36").Value
    QUOTE]

    sorry this is in the Initialize event on the form

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,317

    Re: Listbox to String

    Nicely contrived.
    Now try this:
    Sub snb()
    '  ListBox1.List = Split("aa|bb|cc|dd", "|")
    '  sn = Join(Application.Transpose(ListBox1.List), "|")
        
    '  ListBox1.List = Array("aa", "bb", "cc", "dd")
    '  sn = Join(Application.Transpose(ListBox1.List), "|")
        
      ListBox1.Clear
      ListBox1.AddItem "aa"
      ListBox1.AddItem "bb"
      sn = Join(Application.Transpose(ListBox1.List), "|")
    End Sub
    Last edited by romperstomper; 07-07-2011 at 05:01 AM.

  9. #9
    Forum Contributor FRIEL's Avatar
    Join Date
    03-07-2008
    Location
    Coventry
    MS-Off Ver
    2003 and 2007
    Posts
    266

    Re: Listbox to String

    ok having a play with that.

    got my head round the examples. i think.
    so transpose only works when the listbox is populated in one of the above ways?

    so will i be stuck with the loop?

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,317

    Re: Listbox to String

    Since you haven't told us how you are populating it, we can't really say.

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,317

    Re: Listbox to String

    Then you need to use Join and application.Transpose as shown.

  12. #12
    Forum Contributor FRIEL's Avatar
    Join Date
    03-07-2008
    Location
    Coventry
    MS-Off Ver
    2003 and 2007
    Posts
    266

    Re: Listbox to String

    =/

    any reason this wont work?

    recs = Join(Application.Transpose(ListBox2.List), "|")

  13. #13
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,317

    Re: Listbox to String

    Depends - how is Listbox2 being populated?

  14. #14
    Forum Contributor FRIEL's Avatar
    Join Date
    03-07-2008
    Location
    Coventry
    MS-Off Ver
    2003 and 2007
    Posts
    266

    Re: Listbox to String

    =S

    ok lil wired
    the above works now
    it wouldnt work before.

    thanks guys

    I'd add Rep
    but i added Rep to you last time and it wont let me add to you again lol

  15. #15
    Forum Contributor FRIEL's Avatar
    Join Date
    03-07-2008
    Location
    Coventry
    MS-Off Ver
    2003 and 2007
    Posts
    266

    Re: Listbox to String

    =S Now it dosent!

    this is kinda frustrating.....

    Populated by
    ListBox1.List = ThisWorkbook.Sheets("EMAIL").Range("A2:A36").Value
    in the Initialize event

  16. #16
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,317

    Re: Listbox to String

    You refer to Listbox1 in one code and Listbox2 in the other - which is it? Also, it helps if you tell us what error you are getting: we always want more information than "it doesn't work"

    I don't really care about the rep, though I thank you for the thought.

  17. #17
    Forum Contributor FRIEL's Avatar
    Join Date
    03-07-2008
    Location
    Coventry
    MS-Off Ver
    2003 and 2007
    Posts
    266

    Re: Listbox to String

    Private Sub CommandButton1_Click()
      If ListBox1.ListIndex > -1 Then
        ListBox2.AddItem ListBox1.Value
        ListBox1.RemoveItem ListBox1.ListIndex
        With Cells(1, 30).Resize(ListBox1.ListCount)
          .Value = ListBox1.List
          .Sort Cells(1, 30)
           ListBox1.List = .Value
        End With
      End If
    End Sub
    like this
    what an idiot.
    i totaly missed that

    Run-time error '13':
    Type Mismatch

    sorry for the confusion

  18. #18
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,317

    Re: Listbox to String

    Since you are populating Listbox2 with additem, you'll have to loop.

  19. #19
    Forum Contributor FRIEL's Avatar
    Join Date
    03-07-2008
    Location
    Coventry
    MS-Off Ver
    2003 and 2007
    Posts
    266

    Re: Listbox to String

    ok
    thanks for your help

    at least i have learned somthing =D

  20. #20
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Listbox to String

    Sub snb()
      ListBox2.Clear
      ListBox2.AddItem "aa"
      ListBox2.AddItem 4
      ListBox2.AddItem #12/9/2011#
      
      Cells(10, 1).Resize(UBound(ListBox2.List) + 1, UBound(ListBox2.List, 2) + 1) = ListBox2.List
      sn = Join(Application.Transpose(Cells(10, 1).Resize(UBound
    (ListBox2.List) + 1)), "|")
    End Sub

  21. #21
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,317

    Re: Listbox to String

    I assume you don't expect a response to that?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1