Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 25

Thread: Populate ComboBox with contents of ListBox

  1. #1
    VBAX Regular
    Joined
    Apr 2010
    Location
    Cheltenham
    Posts
    32
    Location

    Populate ComboBox with contents of ListBox

    Hi VBAX

    I need to populate a ComboBox with the contents of the ListBox.

    Ive tried this:

    Me.ComboBox1.ListFillRange = ListBox5
    It doenst throw any errors but nothing shows on the list box, the form isnt open with the lists on so im not sure if thats where the problem is? Any help would be appreciated!!

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
     
    For i = 0 To ListBox5.ListCount - 1
        ComboBox1.AddItem ListBox5.List(i)
    Next
    Last edited by Aussiebear; 12-20-2024 at 05:00 PM.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    I'm gonna piggypack this one

    I am working on a similar problem and I tried this solution but I'm getting the error "Run-time error '381': Could not get the List property. Invalid property array index."

    What it does is when I click the "add" button that transfers the selected item in ListBox1 to ListBox2, it is currently transferring the entire list in ListBox1 to ListBox2. Here is my code:

    Private Sub List1AddButton_Click()
        Dim i As Variant
        For i = 0 To ListBox1.ListCount
            ListBox2.AddItem ListBox1.List(i)
        Next i
    End Sub
    Last edited by Aussiebear; 12-20-2024 at 05:01 PM.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    Apologies,
    It should be
     
    For i = 0 To ListBox1.ListCount - 1
    Last edited by Aussiebear; 12-20-2024 at 05:01 PM.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Apr 2010
    Posts
    25
    Location
    I'm going to jump on this too as its kind of related.

    what is the best way to retrieve the selected values in a listbox to then use those values to search and destroy rows or columns in the workbook?

    Edit: Can someone also please point me to a good Listbox tutorial? i've tried googling but all it does is tell me how to put stuff in to a listbox not deal with the selected values.

  6. #6
    I just figured it out. Here's how I do it:

    Private Sub List1AddButton_Click()
        Dim i As Variant
        If ListBox1.ListIndex = -1 Then Exit Sub
        For i = 0 To ListBox2.ListCount - 1
            If ListBox1.Value = List2.List(i) Then
                Exit Sub
            End If
       Next i
        ListBox2.AddItem ListBox1.List(i)
    End Sub
    Last edited by Aussiebear; 12-20-2024 at 05:02 PM.

  7. #7
    Actually I'm still interested in Shortz's question so I'll keep an eye out.

    By the way, for some reason, Excel 2007 always wants me to define "i" -- it's probably something i'm doing wrong.

  8. #8
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Simpler:
    
    Private Sub CommandButton1_Click()
         ComboBox1.List = ListBox1.List
    End Sub
    Last edited by Aussiebear; 12-20-2024 at 05:03 PM.

  9. #9
    Okay, so I realized today that the code I worked out doesn't work for what I need to do. I need to be able to copy the selected item from Box 1 to Box 2. I want the user to be able to select multiple items at one time. Right now, if I try to call the .Value property of the listbox while it's MultiSelect property is set to fmMultiSelectMulti, it returns errors. It only works if I set it to single.

    Here's what I have right now:

        Dim i As Variant
        If TListBox2.ListIndex = -1 Then Exit Sub
        For i = 0 To List2.ListCount - 1
        If TListBox2.Value = List2.List(i) Then
            Beep
            Exit Sub
        End If
        Next i
        List2.AddItem TListBox2.Value
    I tried to replace "value" with Selected(0) but it doesn't seem to work... Any ideas?
    Last edited by Aussiebear; 12-20-2024 at 05:03 PM.

  10. #10
    Maybe helps!

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    Using Omnibuster's form, we can skip the intermediate list
     
    Option Explicit
    
    Private Sub UserForm_Activate()
        Dim sh As Worksheet
        Set sh = Sheets(1)
        With sh
            ListBox1.List() = Range(.Cells(1, 1), .Cells(Rows.Count, 1).End(xlUp)).Value
        End With
    End Sub
     
    Private Sub CommandButton4_Click()
        Dim i As Long
        With Me.ListBox1
            For i = 0 To .ListCount - 1
                If .Selected(i) Then ComboBox1.AddItem .List(i)
            Next
        End With
    End Sub
    Last edited by Aussiebear; 12-20-2024 at 05:04 PM.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,776
    How about
    Private Sub CommandButton1_Click()
            ComboBox1.List = ListBox1.List
    End Sub
    Last edited by Aussiebear; 12-20-2024 at 05:05 PM.

  13. #13
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    Quote Originally Posted by mikerickson
    How about
    Private Sub CommandButton1_Click()
        ComboBox1.List = ListBox1.List
    End Sub
    Hi Mike,
    The same was offered in post #8, but seems that it has not helped - the task was changed a bit
    Vladimir
    Last edited by Aussiebear; 12-20-2024 at 05:06 PM.

  14. #14
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,776
    True


    I should finish my coffee before posting.

  15. #15
    VBAX Contributor
    Joined
    Dec 2009
    Location
    Sevastopol
    Posts
    150
    Location
    After rereading of this thread with a cup of coffee it is my understanding of the modified task: the multi-selected items of ListBox1 have to be added into ListBox2.
    And I guess that Listbox2 list shall be unique and sorted, because without it the example is present in VBA-help.
    If so then example is attached. It’s UserForm1 code:
    
    ' Copy all the code into UserForm with ListBox1, Lisbox2 and CommandButton1
    ' Put some test items into ListBox1
    
    Private Sub UserForm_Initialize()
         ListBox1.List = Array("Item1", "Item2", "Item3", "Item4", "Item5")
    End Sub
    
    ' Copy selected items of Listbox1 into Listbox2
    Private Sub CommandButton1_Click()
         Lb1SelectedToLb2 ListBox1, ListBox2
    End Sub
    
    ' Copy selected items of Lb1 into Lb2, make it unique, sort and place into Lb2
    Sub Lb1SelectedToLb2(Lb1 As MSForms.ListBox, Lb2 As MSForms.ListBox)
       Dim a(), i&, j&, s$
       On Error Resume Next
       With New Collection
           ' Sort unique items of Lb2
           For j = 0 To Lb2.ListCount - 1
               s = Trim(Lb2.List(j))
               If IsEmpty(.Item(s)) Then
                   ' Thanks to PGC01 for suggested fast sorting method for collection!
                   For i = 1 To .Count
                       If s < .Item(i) Then Exit For
                   Next
                   If i > .Count Then .Add s, s Else .Add s, s, Before:=i
                  End If
              Next
              ' Add as unique the selected items of Lb1 into Lb2
              For j = 0 To Lb1.ListCount - 1
                  If Lb1.Selected(j) Then
                       s = Trim(Lb1.List(j))
                      If IsEmpty(.Item(s)) Then
                          For i = 1 To .Count
                               If s < .Item(i) Then Exit For
                          Next
                          If i > .Count Then .Add s, s Else .Add s, s, Before:=i
                          End If
                          Lb1.Selected(j) = False
                     End If
                 Next
                 ' Copy Collection items into array
                ReDim a(1 To .Count)
                For i = 1 To .Count
                    a(i) = .Item(i)
                Next
            End With
            ' Put unique sorting items into Lb2
           Lb2.List = a()
    End Sub
    Vladimir
    Last edited by Aussiebear; 12-20-2024 at 05:12 PM.

  16. #16
    VBAX Regular
    Joined
    Apr 2010
    Location
    Cheltenham
    Posts
    32
    Location
    Quote Originally Posted by mdmackillop
     
    For i = 0 To ListBox5.ListCount - 1
        ComboBox1.AddItem ListBox5.List(i)
    Next
    Thanks mdmackillop, I tried suggest code but I get runtime 424 "object required".

    Ive tried manipulating most of the ways above but none work.

    Dim i As Variant
    For i = 0 To ListBox5.ListCount - 1
        ComboBox15.AddItem ListBox5.List(i)
    Next i
    Last edited by Aussiebear; 12-20-2024 at 05:12 PM.

  17. #17
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    Please post a copy of your workbook.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  18. #18
    VBAX Regular
    Joined
    Apr 2010
    Location
    Cheltenham
    Posts
    32
    Location
    Quote Originally Posted by mdmackillop
    Please post a copy of your workbook.
    Workbook attached. Stripped version of my original doc but effectively the same. Code is in Form "CustSelector"

  19. #19
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,476
    Location
    The basic problems appears to be that your form contains no comboboxes.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  20. #20
    VBAX Regular
    Joined
    Apr 2010
    Location
    Cheltenham
    Posts
    32
    Location
    Quote Originally Posted by mdmackillop
    The basic problems appears to be that your form contains no comboboxes.
    ahhh, Probably my fault for not explaining properly, the ComboBox is embeded onto the sheet "Contract".

Posting Permissions

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