Consulting

Results 1 to 14 of 14

Thread: Sleeper: Multiple Value List Box

  1. #1
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location

    Question Sleeper: Multiple Value List Box

    How do I copy all multiple selected string value in a List Box into Excel Worksheet?

    For example, to copy all multiple selected names in the list box to a column in "Sheet1"?

    Thanks.

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Where is the listbox located?

  3. #3
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    UserForm1.ListBox1

    How to use .Selected or something else to read selected data?

  4. #4
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    How about something like this (untested):

    Dim i as integer, seltotal as integer, temp as variant
    ' Count number of selections in ListBox1
    ' I prefer this to using ReDim Preserve on each loop cycle
    seltotal = 0
        For i = 0 To ListBox.ListCount - 1
           If ListBox1.Selected(i) Then seltotal = seltotal + 1
        Next i
    ' Stuff an array with all of the selections
    ReDim temp(1 to seltotal, 1 to 1)
    seltotal = 1
    for i = 0 to ListBox1.ListCount - 1
    if ListBox1.Selected(i) then
    temp(seltotal, 1) = ListBox1.List(i)
    seltotal = seltotal + 1
    end if
    next i
    ' Dump the array to ActiveSheet
    ActiveSheet.Cells(1,1).Resize(ubound(temp,1),ubound(temp,2)) = temp
    If you have multiple columns in the listbox, you'll have to include a column number in .List().

  5. #5
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    The code still not work. How?
    How to save selected string in the ListBox to an array?
    How do I filter the data in listbox using text entered in TextBox1?
    eg. enter "o" will focus on first string start with "o" in ListBox.

    Thanks.

  6. #6
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    Oops, that's supposed to be
    For i = 0 to ListBox1.ListCount
    (missing the '1' in the code above.)

    I still haven't tested the code, so if it's not working let me know what happened.

    Do you have multiple columns in ListBox1?

  7. #7
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    no multiple columns. thanks. I had already put inthe '1'. Working but copy all to the Sheet without control.

  8. #8
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    Quote Originally Posted by sheeeng
    no multiple columns. thanks. I had already put inthe '1'. Working but copy all to the Sheet without control.
    What control is lacking?

  9. #9
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Copy all the data without string compare.

  10. #10
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    268
    Location
    OK, maybe I'm misunderstanding this. Let me tell you what I think you were asking for, and then you can correct me.

    You have a ListBox on a userform. It contains multiple items, and allows for selecting multiple items. You want to copy all of the user-selected items (to the worksheet), leaving behind all of the items that are not selected. For example:

    LISTBOX		      SELECTED       SHEET
    A			yes	    A
    B			 no	      D
    C	---> 	        no     -->    F
    D			yes
    E			no
    F			yes

  11. #11
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Quote Originally Posted by sheeeng
    Copy all the data without string compare.
    What 'string compare' are you referring to?

  12. #12
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location

    Lightbulb

    BlueCactus, you are almost right! Thanks.

    Let me layout my need in this VBA sub.

    But before copy to Sheet2, it must check whether .Selected exist in sheet1. If exist in Sheet1, then copy it to sheet2.


    Thanks a lot.

  13. #13
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    New question here. Can anyone show me how to built a 5/6 column Multiple Column List Box?

    Thanks.

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by sheeeng
    New question here. Can anyone show me how to built a 5/6 column Multiple Column List Box?
    You need to set the columncount property and load all columns. This can be done from a range, set the RowSource property to that 2D range, or by loading from an array

    ReDim ary(1 To 2, 1 To 5)
    For i = 1 To 2
    For j = 1 To 5
    ary(i, j) = i * j
    Next j
    Next i
    With ListBox1
    For i = 1 To 2
    .AddItem ary(i, 1)
    For j = 2 To 5
    .List(.ListCount - 1, j - 1) = ary(i, j)
    Next j
    Next i
    End With

    or directly

    ListBox1.List = ary

    If your data is in a Recordset, you can convert this to an array and load

    ary = RS.GetRows() 
    With Me.ListBox1 
    .ColumnCount = 3 
    .List = ary 
    End With
    And to get at the data, use


    With Me.ListBox1 
            MsgBox .List(.ListIndex, 0) & ", " & _ 
                 .List(.ListIndex, 1) & ", " & _ 
                 .List(.ListIndex, 2) 
        End With

Posting Permissions

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