+ Reply to Thread
Results 1 to 6 of 6

Setting up a search macro to find multiple words

Hybrid View

O0J0E Setting up a search macro to... 03-16-2012, 08:42 AM
mrice Re: Setting up a search macro... 03-16-2012, 09:35 AM
O0J0E Re: Setting up a search macro... 03-16-2012, 12:25 PM
O0J0E Re: Setting up a search macro... 03-16-2012, 02:08 PM
O0J0E Re: Setting up a search macro... 03-16-2012, 03:22 PM
Tinapie Re: Setting up a search macro... 09-19-2013, 05:14 AM
  1. #1
    Registered User
    Join Date
    03-08-2012
    Location
    New Hampshire
    MS-Off Ver
    2010
    Posts
    12

    Setting up a search macro to find multiple words

    Hello, I need some addition help with a search macro.

    What I have
    I currently have a search macro set up the searches column A for what you have typed into H5, if it finds the value in H5 in column A it copies columns A to F and pastes that info into H12- M12 (first it will clear all contents in these cells to avoid showing the wrong results) The macro is written as follows:
      Sub Search()
    Range("H11").CurrentRegion.ClearContents: Range("H11") = "Results"
    FindWhat = (Range("H5"))
    For Each Cell In Range("A2", Range("A" & Rows.Count).End(xlUp))
    If InStr(Cell, FindWhat) <> 0 Then
    Cell.Offset(, 0).Resize(, 7).Copy Range("H" & Rows.Count).End(xlUp).Offset(1, 0)
    End If
    Next Cell
    End Sub

    What I need
    The macro only searches for single values for example if I type in lion I will get all results with lion involved but if I type in angry lion I will show no results even though in column A I have lion, cat, angry, fierce. Also if i type in Lion I will get nothing due to the upper case.
    1. Is there a way to search for both upper and lower case values?
    2. How can I search for multiple words at once that are not right next to each other?

    any help would be greatly appreciated, thanks.

    -Joe.
    Last edited by Leith Ross; 03-16-2012 at 06:13 PM. Reason: Added Code Tags

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: Setting up a search macro to find multiple words

    Try the following

    Sub Search2()
    Dim MyArray() As String
    Dim N As Long
    Dim SearchRange As Range
    Dim FoundCell As Range
    
    Set SearchRange = Range("A2", Range("A" & Rows.Count).End(xlUp))
    MyArray = Split(Range("H5"), " ")
    For N = 0 To UBound(MyArray)
        If WorksheetFunction.CountIf(SearchRange, MyArray(N)) > 0 Then
            Set FoundCell = SearchRange.Find(MyArray(N), , xlValues, xlWhole)
            FoundCell.Offset(, 0).Resize(, 7).Copy Range("H" & Rows.Count).End(xlUp).Offset(1, 0)
        End If
    Next N
    End Sub
    I've assumed that words in the search term will be separated by a space.
    Martin

  3. #3
    Registered User
    Join Date
    03-08-2012
    Location
    New Hampshire
    MS-Off Ver
    2010
    Posts
    12

    Re: Setting up a search macro to find multiple words

    I had to modify this a bit but this was more what I was after, thanks for the reply here is the code I used:

    Sub Search2()
    Dim MyArray() As String
    Dim N As Long
    Dim SearchRange As Range
    Dim FoundCell As Range
    
    Range("H11").CurrentRegion.ClearContents: Range("H11") = "Results"
    
    MyArray = Split(Range("H5"), " ")
    For Each Cell In Range("A2", Range("A" & Rows.Count).End(xlUp))
    For N = 0 To UBound(MyArray)
    If InStr(Cell, MyArray(N)) <> 0 Then
    Cell.Offset(, 0).Resize(, 7).Copy Range("H" & Rows.Count).End(xlUp).Offset(1, 0)
    End If
    Next N
    Next Cell
    
    
    End Sub
    Thanks again!

  4. #4
    Registered User
    Join Date
    03-08-2012
    Location
    New Hampshire
    MS-Off Ver
    2010
    Posts
    12

    Re: Setting up a search macro to find multiple words

    only thing I forgot was to set something for Both uppercase and lower case, working on it now.

  5. #5
    Registered User
    Join Date
    03-08-2012
    Location
    New Hampshire
    MS-Off Ver
    2010
    Posts
    12

    Re: Setting up a search macro to find multiple words

    added

    If InStr(1, Cell, MyArray(N), vbTextCompare) <> 0 Then

    fixed everything

  6. #6
    Registered User
    Join Date
    09-19-2013
    Location
    Makati, Philippines
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Setting up a search macro to find multiple words

    hi why this macro not working in my sheet.. im using excel 2010

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1