PDA

View Full Version : Add items from Comboxes to Listbox



sindrefm
06-03-2015, 01:42 AM
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.


13591

Can someone help with this?

Thanks.

jonh
06-03-2015, 03:49 AM
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.

sindrefm
06-05-2015, 12:34 AM
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.

jonh
06-05-2015, 02:11 AM
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

sindrefm
06-05-2015, 03:18 AM
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

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

jonh
06-05-2015, 07:21 AM
So, is there a reason you can't reformat the data as I've shown above?

sindrefm
06-05-2015, 11:00 AM
I can't get it to work.. nothing is showing in ListBox2.

jonh
06-05-2015, 11:08 PM
Attach an example file.

sindrefm
06-08-2015, 07:01 AM
See attachment.

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