Populating Listbox based on ComboBox selection - VBA

JackDomino1

New Member
Joined
Apr 7, 2020
Messages
39
Office Version
  1. 365
Platform
  1. Windows
Hi,

Im looking to populate a list box (5 coloums) based on the selection a user makes in a combobox.

However, the options in the combobox are not unique, so there will be multiple sets of data to display.

NameAgeHair Colour
Jack
34​
Brown
Ben
23​
Blonde
Steve
45​
Grey
Jack
65​
Black
John
19​
Black
Ben
21​
Brown

Names are select-able in the combobox. But if the user selects "Jack", the lstbox needs to display two rows of data.

I have managed to make it return the information associated to the first entry which matches the selection criteria, but I am struggling to get it to work on multiple entries.

Thanks
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
How about
VBA Code:
Private Sub ComboBox1_Click()
   Dim Lst As Variant, Rws As Variant
   
   With Sheets("Main").Range("A1").CurrentRegion
      Rws = Filter(Evaluate(Replace("transpose(if(@=" & Chr(34) & Me.ComboBox1 & Chr(34) & ",row(@),false))", "@", .Columns(1).Address)), False, False)
      Lst = Application.Index(.Value2, Application.Transpose(Rws), Array(1, 2, 3, 4, 5))
      If UBound(Rws) = 0 Then Lst = .Rows(Rws(0)).Value
   End With
   Me.ListBox1.List = Lst
End Sub
 
Upvote 0
Thank you, just tested it out.

Lst = Application.Index(.Value2, Application.Transpose(Rws), Array(1, 2, 3, 4, 5))

This line is flagging runtime error "13"
 
Upvote 0
Are your names in col A starting in A2 with a header in row1?
Also do you have any blank rows or columns in the data?
 
Upvote 0
I would just put the same list in both controls and alter the .ColumnWidths properties.

VBA Code:
Private Sub Userform_Initialize()
    ComboBox1.ColumnCount = 2
    ListBox1.ColumnCount = 3
    ListBox1.List = Range("A1:C6").Value
    ComboBox1.List = Range("A1:C6").Value
    ComboBox1.ColumnWidths = ";0;0"
    ListBox1.ColumnWidths = "0;;"
End Sub

Private Sub ComboBox1_Change()
    ListBox1.ListIndex = ComboBox1.ListIndex
End Sub
 
Upvote 0
How are you loading the combobox?
 
Upvote 0
How are you loading the combobox?
Private Sub UserForm_Initialize()
'populates combobox with avaliable XP
Dim z As Integer
z = 3
While ActiveWorkbook.Sheets("Data").Cells(z, 2) <> ""
UserForm14.ComboBox1.AddItem (ActiveWorkbook.Sheets("Data").Cells(z, 2))
z = z + 1
Wend
End Sub
 
Upvote 0
You're loading the combo from col B, but you said your names were in col A. :unsure:
 
Upvote 0
You're loading the combo from col B, but you said your names were in col A. :unsure:
Yes, so the names in Col A are not unique and I didn't want duplicates appearing in the combobox. So i created a separate, unique list to populate the combobox with (stored in a different sheet). This is probably an overly complicated way to do it.

However, bypassing this and using Col A to populate the combobox still flags the same error
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top