Consulting

Results 1 to 9 of 9

Thread: Change code to search multiple terms across columns

  1. #1

    Change code to search multiple terms across columns

    Hello,

    I have this code that I need modified to search more than one term at a time. I need it to search everything from columns A to P.

    Here is the code:

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("B2")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Dim sAddr As String
    Dim LastRow1 As Long
    LastRow1 = Sheets("Search").Range("A" & Rows.Count).End(xlUp).Row + 1
    Dim LastRow2 As Long
    LastRow2 = Sheets("2014-2019").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Sheets("Search").Range("A5:P" & LastRow1).ClearContents
    Dim searchVal As Range
    With Sheets("2014-2019").Range("A2:P" & LastRow2)
    Set searchVal = .Find(Target, LookIn:=xlValues, LookAt:=xlPart)
    If Not searchVal Is Nothing Then
    sAddr = searchVal.Address
    Do
    searchVal.EntireRow.Copy
    ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
    Set searchVal = .FindNext(searchVal)
    Loop While searchVal.Address <> sAddr
    sAddr = ""
    End If
    End With
    Set searchVal = Nothing
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    End Sub

    Thanks in advance

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Quote Originally Posted by earthandbody View Post
    I have this code that I need modified to search more than one term at a time.
    At the moment it searches columns A to P for the term in cell B2. Where are the other terms sought for located?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    Quote Originally Posted by p45cal View Post
    At the moment it searches columns A to P for the term in cell B2. Where are the other terms sought for located?
    The columns have data like delivery location, description, vendor, buyer name, etc. I may have pieces of each and want to enter what I have and have it search and find the rows that contain all of the search string.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    Supply a workbook with sample data.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Sample file with data.
    Attached Files Attached Files

  6. #6
    VBAX Mentor 大灰狼1976's Avatar
    Joined
    Dec 2018
    Location
    SuZhou China
    Posts
    479
    Location
    Hi earthandbody! If my understanding is correct...
    The content of B2 is separated by a comma.
    for example: B2 = ROCK,JOHNSON
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Intersect(Target, Range("B2")) Is Nothing Then Exit Sub
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        Dim sAddr As String
        Dim LastRow1 As Long
        LastRow1 = Sheets("Search").Range("A" & Rows.Count).End(xlUp).Row + 1
        Dim LastRow2 As Long
        LastRow2 = Sheets("2014-2019").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        Sheets("Search").Range("A5:P" & LastRow1).ClearContents
        Dim searchVal As Range, arr, i&, j&
        With Sheets("2014-2019")
            arr = Split(Target, ",")
            For i = 2 To LastRow2
                For j = 0 To UBound(arr)
                    Set searchVal = .Rows(i).Find(arr(j), LookIn:=xlValues, LookAt:=xlPart)
                    If Not searchVal Is Nothing Then
                        searchVal.EntireRow.Copy
                        ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
                        Exit For
                    End If
                Next j
            Next i
        End With
        Set searchVal = Nothing
        Application.CutCopyMode = False
        Application.EnableEvents = True
        Application.ScreenUpdating = True
    End Sub

  7. #7
    A lot of the data will be separated by commas.

  8. #8

  9. #9
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,875
    See if the attached is any use (Sheet Search2).

    Regarding cross posting see http://www.excelguru.ca/content.php?184
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Posting Permissions

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