AdvancedSearch module to search for more than 1 word?

KrisNey

New Member
Joined
Nov 1, 2018
Messages
5
Hello, first time posting...I borrowed a bit of code to create a search box in cell B1. This allows me to search in the Range for a keyword, and it returns matching rows.

I would like to know how to expand this, in order to search for two or three keywords.

Thank you!

Code:
Sub MyAdvancedSearch()
Application.ScreenUpdating = False

    Dim r As Range, v As String, s As String
    With Sheets("August")
        .Range("c2").CurrentRegion.EntireRow.Hidden = False
        s = LCase(.Range("b1").Value)
        For Each r In .Range("c4", .Range("c" & .Rows.Count).End(xlUp))
        v = ""
        v = v & r.Value
        v = v & r.Offset(0, 1).Value
        v = v & r.Offset(0, 2).Value
        v = v & r.Offset(0, 3).Value
        v = v & r.Offset(0, 4).Value
        v = LCase(v)
        If Not v Like "*" & s & "*" Then
            r.EntireRow.Hidden = True
        
        End If
    Next r
End With
Application.ScreenUpdating = True
    
End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi
Welcome to the board

Do you mean the row must have

both keywords

or

one of the 2 keywords

?
 
Upvote 0
Hi KresNey,

I guess this does the trick

Code:
=isnumber(search($F$2,C2))*isnumber(search($G$2,C2))

where $f$2 and $g$2 are the words to looking for and C2, C3 and so on are the cells with text to be searched

On cells where both words are met, the function returns 1 whereas none or just one of the words are met the function returns 0, so you notice that cell does not meet your requirements.

Hope this helps and welcome to the board.

Vandalo
 
Upvote 0
Hi

Try replacing the test with:

Code:
If Not ((v Like "*" & s1 & "*") And (v Like "*" & s2 & "*")) Then

where s1 and s2 are the 2 keywords
 
Upvote 0
Hello, I am having trouble again. Tried to recreate my sheet, but my 2 keyword search is not working.
Any help is appreciated.

Code:
Sub MyAdvancedSearch()
Application.ScreenUpdating = False

    Dim r As Range, v As String, s As String
    With Sheets("Sheet1")
    .Range("c2").CurrentRegion.EntireRow.Hidden = False
    s = LCase(.Range("b1").Value)
    For Each r In .Range("c3", .Range("c" & .Rows.Count).End(xlUp))
    v = ""
    v = v & r.Value
    v = v & r.Offset(0, 1).Value
    v = v & r.Offset(0, 2).Value
    v = v & r.Offset(0, 3).Value
    v = v & r.Offset(0, 4).Value
    v = LCase(v)
    If Not ((v Like "*" & s1 & "*") And (v Like "*" & s2 & "*")) Then
        r.EntireRow.Hidden = True
    End If
Next r
End With
Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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