Link to home
Start Free TrialLog in
Avatar of dpabd
dpabd

asked on

Return Filtered Results only

Experts,

I have an excel spreadsheet with data in. I am attempting to create a search facility that would allow me to enter a few keywords and then use those keywords to seacrh a column in my spreadsheet to see if they appear in that column. If any of the keywords appear in ANY of the columns, then I would like the spreadsheet to filter on those rows.  I.e. if any of the cells in that column (lets say column d) contains any of the keywords, the end result should be that all those rows are filtered and displayed to the user.  So at the end, after I have run the macro, the spreadsheet will show all rows where in column d, contains any of the keywords entered.

Thanks
Avatar of dvivek_aca
dvivek_aca
Flag of India image

this was basically a modified version of a solution i gave somtime back in EE...all you have to do for you to work
1. run the macro Auto_Filter
2. type the keyword in the first text box
3. Press Filter..

Please let know if your requirement is something different
a stripped down version with textbox to search for keywords only

http://www.geocities.com/dvivek_aca/Q_21740621_v2.xls
Avatar of Wayne Taylor (webtubbs)
Hi dpabd,

Here is a macro that will ask for your search item and filter the results appropriately....

Sub FilterSearch()
    Dim SearchCriteria As String
    SearchCriteria = InputBox("Enter your required Search Criteria")
    If SearchCriteria = "" Then Exit Sub
    [D1].AutoFilter Field:=4, Criteria1:="*" & SearchCriteria & "*"
End Sub

It assumes the first column is A.

Cheers,

Wayne
I should mention that this will allow only ONE keyword. Having more than 1 keyword is possible, but the results will have to be displayed on a seperate sheet.

Wayne
Below is a macro that will allow multiple search items (seperated with a ; ), and hide any rows that don't contain any of the items in column D....

Sub ShowMatches()
     Dim r As Long
     Dim LastRow As Long
     Dim SearchCriteria As String
     Dim arr() As String
     Dim i As Long
     Dim HideRow As Boolean

     SearchCriteria = InputBox("Enter your required Search Criteria" & vbCrLf & _
                               vbCrLf & "[Seperate Search items with a semi-colon ( ; )]")
     If SearchCriteria = "" Then Exit Sub
     Application.ScreenUpdating = False
     arr = Split(SearchCriteria, ";")
     LastRow = [D65536].End(xlUp).Row
     For r = LastRow To 2 Step -1
          For i = LBound(arr) To UBound(arr)
               If InStr(1, Cells(r, "D"), arr(i)) = 0 Then
                    HideRow = True
               End If
          Next
          Rows(r).Hidden = HideRow
          HideRow = False
     Next
     Application.ScreenUpdating = True
End Sub
Avatar of dpabd
dpabd

ASKER

Webtubbs - thats great thanks.  It does almost exactly what I want.  

Except, when I search multiple items, it only returns rows which have ALL the keywords I
enter.  Is it possible to do a search where I enter multiple keywords and it
returns any rows which has ANY of the keywords?

Thanks.
Hi dpabd,

Perhaps try this macro....

Sub ShowMatches()
     Dim r As Long
     Dim LastRow As Long
     Dim SearchCriteria As String
     Dim arr() As String
     Dim i As Long
     Dim HideRow As Boolean

     SearchCriteria = InputBox("Enter your required Search Criteria" & vbCrLf & _
                               vbCrLf & "[Seperate Search items with a semi-colon ( ; )]")
     If SearchCriteria = "" Then Exit Sub
     Application.ScreenUpdating = False
     arr = Split(SearchCriteria, ";")
     LastRow = [D65536].End(xlUp).Row
     For r = LastRow To 2 Step -1
          For i = LBound(arr) To UBound(arr)
               If InStr(1, Cells(r, "D"), arr(i)) > 0 Then
                    HideRow = False
               End If
          Next
          Rows(r).Hidden = HideRow
          HideRow = True
     Next
     Application.ScreenUpdating = True
End Sub

Regards,

Wayne
Avatar of dpabd

ASKER

thanks, almost there now!  However, the only problem now is that the last row
always gets returned no matter what you search on.
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dpabd

ASKER

This is great, thanks.
No problem. Glad I could help :)

BTW - I should be thanking you! Your question just got me my Wizard certificate in Excel :)