• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

filter listbox

Belleke

Well-Known Member
I am using this code to filter a listbox (column A) This works perfect.
Code:
Private Sub Commandbutton1_Click()
For i = ListBox1.ListCount - 1 To 0 Step -1
    If InStr(1, ListBox1.List(i), TextBox1) = 0 Then ListBox1.RemoveItem (i)
Next
End Sub
But I want to filter on Column B And E as well, but whatever I try the results i get are not correct.
Anybody suggestions?
Thanks in advance.
 
Belleke
Do other columns ListBoxes has correct lists before 'filtering'?
Do other columns 'filtering' use ListBox1?
Others could have another 'names' ( or it depends, how have You done those...).
 
Hi vletm
see attached
Thanks for our time
 

Attachments

  • listbox filter.xlsm
    48.9 KB · Views: 11
Belleke
Should that file include Your #1 case?
Where/how would You do Your 'filtering'?
Would You give some hints?
Ps. There are 'some' ActiveX-components, which won't work her at all.
 
Sorry about the active X (are you working on a Mac?)
The purpose of this file is just learning purposes to filter a listbox.
The result I want to achieve is
Filter column A (working) when i type in textbox 1 or filter column B when i type in textbox 2 or filter column E when i type in textbox 3
Any suggestions are welcome.
 
Belleke
Yes, 'Mac' rules!
Your 'List' has two dimensions.
You missed form .List(i) that another dimension eg .list(i,0)
Check below samples ...

Code:
Private Sub Cmd_00_Click()
    For i = LB_00.ListCount - 1 To 0 Step -1
        If InStr(1, LB_00.List(i, 0), T_00) = 0 Then LB_00.RemoveItem (i)
    Next
End Sub

Private Sub Cmd_01_Click()
    For i = LB_00.ListCount - 1 To 0 Step -1
        If InStr(1, LB_00.List(i, 1), T_01) = 0 Then LB_00.RemoveItem (i)
    Next
End Sub
 
Thanks vletm
problem solved.
I can hit myself, I tried your solution but forgot that a listbox list starts with column 0:oops:
You should run 'UserForm_Initialize'
I am using LB_00.List = [beer_tbl].Value instead, is there a reason I should prefer UserForm_Initialize?
 
Belleke
... after every 'filtering', You should run 'UserForm_Initialize' .. as You have noticed.
Do You still 'RemoveItems' from that List?
If so then ... what would be there after 'filtering'? ... the result of filtering.
If You would like to continue 'filtering'
from that 'the result' then Okay,
but if You would like to start 'new' filtering
... then maybe better to get all items to List.
Hmm...?
 
Vletm, I understand, that is why I added a reset button where I am using this
Code:
LB_00.ListIndex = -1
    For Each Ctrl In Controls
        If TypeName(Ctrl) = "TextBox" Or TypeName(Ctrl) = "ComboBox" Then Ctrl.Value = ""
    Next Ctrl
    LB_00.List = [beer_tbl].Value
I was just wondering why I should use UserForm_Initialize instead of LB_00.List = [beer_tbl].Value[/CODE]
 
Belleke
Your sample file didn't have a reset button ...
You had that UserForm_Initialize ready there ... why need two routines for that?

> other note ...
Now, You could use one column in time with filtering.
Don't You need to use more than one column filtering?
eg with Kleur = bruin and Brouw N = 10 ... or any combination.
 
Vletm,
Thanks for the other note, ganna play arround a bit with that.
Thanks for your time, have a nice weekend.
 
Back
Top