Consulting

Results 1 to 9 of 9

Thread: Add items from Comboxes to Listbox

  1. #1
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location

    Add items from Comboxes to Listbox

    Hi,

    What I want:

    - Have two Comboxes, ComboBoxA and ComboboxB.
    - ComboBoxA to contain items from a column in Sheet1.
    - ComboBoxB to contain items based on selections in ComboBoxA.
    Ex.
    If UserForm1.ComboBox1.Value = "Car" Then
    UserForm1.ComboBox2.List = Array("BMW", "Ford")


    - Adding content with AddButton from both Comboxes into ListBox1, listed in two columns. See picture.
    - Delete selected content from Listbox1 with DeleteButton.

    - When selecting an item in Listbox1, it should show content from a column in Sheet2 in Listbox2, based on column B in Listbox1. See picture.


    Excel_ComboBox_Listbox.jpg

    Can someone help with this?

    Thanks.

  2. #2
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Private Sub btnAdd_Click()
        If cbo1.ListIndex + cbo2.ListIndex >= 0 Then
            lst1.AddItem cbo1
            lst1.Column(1, lst1.ListCount - 1) = cbo2
        End If
    End Sub
    
    Private Sub btnRemove_Click()
        If lst1.ListIndex <> -1 Then _
            lst1.RemoveItem lst1.ListIndex
    End Sub
    The part about Listbox2 is too vague.

  3. #3
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location
    Thanks you.
    This works very good.

    For the ListBox2 I want the following:

    The listbox2 is only for view.
    When selecting an item in ListBox1 I want to show information about the item based on item from Combobox2.
    If Combobox1 item = "Car" and Combobox2 item is "BMW" in ListBox1, then I want to show information about the "BMW" in ListBox1. This Information comes from a column in a Worksheet.
    Ex.
    Column A:
    Row1: BMW
    Row2: Model 318

    I want Row2 to show in Listbox2 when selecting the Item Car:Bmw in Listbox1.

  4. #4
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Is there a reason why all the data is in one column? Makes filtering harder.

    So I've ignored your format and assumed it should be e.g.

    Make | Model
    ___________
    Ford | Focus
    Ford | Fiesta
    BMW | 318
    VW | Golf
    VW | Polo

    (And there's probably a much nicer way of doing this as well...)

    Private Sub lst1_Change()
    On Error Resume Next
        lst2.Clear
        With Sheets("sheet2").UsedRange
            .AutoFilter
            .AutoFilter Field:=1, Criteria1:=lst1.Column(1)
            For Each c In .Offset(1, 1).Resize(.Rows.Count - 1, 1).SpecialCells(xlCellTypeVisible)
                lst2.AddItem c.Text
            Next
        End With
    End Sub

  5. #5
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location
    Sorry, it was some lack of description from my side. I marked with BOLD what was missing.



    For the ListBox2 I want the following:

    The listbox2 is only for view.
    When selecting an item in ListBox1 I want to show information about the item in ListBox2 based on item from Combobox2.
    If Combobox1 item = "Car" and Combobox2 item is "BMW" in ListBox1, then I want to show information about the "BMW" in ListBox2. This Information comes from a column in a Worksheet.
    Ex. in Worksheet

    Excel - Info.jpg
    I want from Row2 and down to show in Listbox2 when selecting the Item Car:Bmw in Listbox1.

  6. #6
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    So, is there a reason you can't reformat the data as I've shown above?

  7. #7
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location
    I can't get it to work.. nothing is showing in ListBox2.

  8. #8
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    Attach an example file.

  9. #9
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location
    See attachment.

    The ListBox1_Click() does not work, but I tried to code it the way I want it to be.
    Attached Files Attached Files

Posting Permissions

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