Filter a listview with combobox

van0912

New Member
Joined
Jul 27, 2022
Messages
16
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone,

I'm trying to filter a listview based on a combobox value (value in range I) but I don't know how to write the syntax and where to add it in my code... Could you please help me on this?

Thank you !

VBA Code:
Private Sub UserForm_Initialize()

Dim ws As Worksheet
Dim rg As Range
Dim i As Long
Dim j As Long

Set ws = ThisWorkbook.Sheets("Test")  
Set rg = ws.Range("ReferenceTab")            

With UserForm3.ListView1             

For i = 1 To 12         
        .ColumnHeaders.Add , , rg.Offset(0, i - 1)
Next i

For i = 1 To 500
        .ListItems.Add , , rg.Offset(i, 0)
Next i

For i = 1 To 500   
For j = 1 To 11    
            .ListItems(i).ListSubItems.Add , , rg.Offset(i, j)
Next j
Next i

    .View = lvwReport

End With

Dim v, e
With Sheets("Test").Range("I4:I500")
v = .Value
End With

With CreateObject("scripting.dictionary")
.comparemode = 1

For Each e In v
If Not .exists(e) Then .Add e, Nothing
Next
If .Count Then Me.ComboBox1.List = Application.Transpose(.Keys)

End With
       
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
If you are making a selection from the combobox, the code to populate the list control would be in the combobox_change event.
I am just looking at the one add item line.
It may not work for you, but you will see one way to check the criteria before adding the item.

VBA Code:
For i = 1 To 500
if instr(rg.Offset(i, 0),combobox1) then
        .ListItems.Add , , rg.Offset(i, 0)
end if
Next i
 
Upvote 0
If you are making a selection from the combobox, the code to populate the list control would be in the combobox_change event.
I am just looking at the one add item line.
It may not work for you, but you will see one way to check the criteria before adding the item.

VBA Code:
For i = 1 To 500
if instr(rg.Offset(i, 0),combobox1) then
        .ListItems.Add , , rg.Offset(i, 0)
end if
Next i
Hello Daves, thanks for replying!

Sorry, I don't really understand how to do as I'm new on VBA... Could you please explain?
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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