Filter listbox based on combobox - with multiple columns in the list box

JohnSmith9945

New Member
Joined
Sep 30, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hey everyone,

I found some code in an old post about how to filter the list box based on a combo selection - however i cant get it to work with multiple columns, how would i ammend the code to make it work?

context: Im trying to use the combo box as a filter for the listbox to show all records that have that ID.

VBA Code:
Dim rngData As Range

Private Sub UserForm_Initialize()
'This Event runs when Userform1 is initialized
'Change here to suit your data
    Set rngData = Sheets("Sheet1").Range("A2:B9")

    'Set unique Option list to ComboBox1
    Me.ComboBox1.List = Array_Unique_Collection(rngData.Columns(1).Value)
End Sub

Private Sub ComboBox1_Change()
'This Event runs when ComboBox1 is changed
    Me.ListBox1.Clear

    For i = 1 To rngData.Rows.Count
        If rngData.Cells(i, 1).Value = ComboBox1.Value Then
            Me.ListBox1.AddItem rngData.Cells(i, 2).Value
        End If
    Next
End Sub

Function Array_Unique_Collection(ByVal NotUniqueArry As Variant) As Variant
'This is a function returns unique collection as a 1D array.
'returns NULL when there is no value
    Dim cTmp As New Collection
    Dim i As Long
    Dim aTmp As Variant
    Dim vElm As Variant

    On Error Resume Next
    For Each vElm In NotUniqueArry
        cTmp.Add CStr(vElm), CStr(vElm)
    Next
    On Error GoTo 0
    If cTmp.Count = 1 And cTmp.Item(1) = vbNullString Then
        Array_Unique_Collection = Null
        Exit Function
    End If
    ReDim aTmp(1 To cTmp.Count)
    For i = 1 To cTmp.Count
        aTmp(i) = cTmp.Item(i)
    Next
    Array_Unique_Collection = aTmp
End Function


Thanks!
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi,
your code is working with two columns (A&B) how many more do you want to include?
Helpful to forum if supply copy of or worksheet with dummy data using MrExcel Addin xl2BB

Dave
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,913
Members
449,093
Latest member
dbomb1414

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