Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Filter Items in listbox using Like or sometype of wilcard search in columns

Posted on 2013-11-07
7
Medium Priority
?
241 Views
Last Modified: 2013-11-12
vba 2010  excel
variable to pass
str = "LOCTITE"

What I'm doing currently :
Removing rows in a listbox that match my criteria on Exact Strings
in this case  
str = "LOCTITE"

Specifically in Columns  8 or 9



What I need to happen now:
My problem is sometimes in Column 8 and/or 9
The column may contain  "LOCTITE CO."  OR  TRUE VALUE LOCTITE"


So i need the code to look in columns 8 and/or 9
If "LOCTITE" exists somewhere in the string .

Removing rows in a listbox that DO NOT CONTAIN my criteria.
in this case  
str = "LOCTITE"

so all i will be left with is rows that DO CONTAIN in Columns 8 and/or 9 the word "LOCTITE"




' get rid of unwanted results that do not contain str
 Dim ri As Long
With UserForm2.ListBox1
    For ri = .ListCount - 1 To 0 Step -1
If .List(ri, 8) <> str Or .List(ri, 9) <> str Then
                    .RemoveItem ri
        End If
    Next
End With

Open in new window






Thanks
fordraiders
0
Comment
Question by:Fordraiders
7 Comments
 
LVL 53

Accepted Solution

by:
Rgonzo1971 earned 1200 total points
ID: 39630906
Hi,

pls try
' get rid of unwanted results that do not contain str
 Dim ri As Long
With UserForm2.ListBox1
    For ri = .ListCount - 1 To 0 Step -1
        If Not(.List(ri, 8) Like "*" & str & "*") _
            Or Not(.List(ri, 9) Like "*" & str & "*") Then
                    .RemoveItem ri
        End If
    Next
End With

Open in new window

Regards
0
 
LVL 31

Assisted Solution

by:gowflow
gowflow earned 400 total points
ID: 39630931
For further refinement and in case you go thru some lowercase/uppercase replace this line
If Not(.List(ri, 8) like "*" & str & "*") Or Not(.List(ri, 9) Like "*" & str & "*") Then

Open in new window


by this line
If Not(lcase(.List(ri, 8)) like "*" & lcase(str) & "*") Or Not(lcase(.List(ri, 9)) Like "*" & lcase(str) & "*") Then

Open in new window


gowflow
0
 
LVL 1

Expert Comment

by:sisiliano
ID: 39631099
If InStr(.List(ri, 8), str) = 0 or  InStr(.List(ri, 9), str) =0 then
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 81

Assisted Solution

by:byundt
byundt earned 400 total points
ID: 39632437
When testing sisiliano's suggestion of InStr, the comparison is case sensitive as posted--so "Loctite" won't match "TRUE VALUE LOCTITE". But you can make a case insensitive comparison with:
If InStr(1, .List(ri, 8), str, vbTextCompare) = 0 Then

Open in new window

0
 
LVL 1

Expert Comment

by:sisiliano
ID: 39632522
Oops didnt think about that.

Hope byundt suggestion works for you though.
0
 
LVL 3

Author Comment

by:Fordraiders
ID: 39643137
goflow,

When testing your solution further.
If Not(lcase(.List(ri, 8)) like "*" & lcase(str) & "*") Or Not(lcase(.List(ri, 9)) Like "*" & lcase(str) & "*") Then
If Loctite is in 8 but not in 9...

It will still remove the line..

If its it one or the other then i want to keep the line.. ?


Thanks
fordraiders
0
 
LVL 81

Expert Comment

by:byundt
ID: 39643180
It might be clearer to use an Else clause:
If (lcase(.List(ri, 8)) like "*" & lcase(str) & "*") Or (lcase(.List(ri, 9)) Like "*" & lcase(str) & "*")  Then
Else
     'Remove the line
End If

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question