Copy the entire line from one sheet to another based on one word in a paragraph inside a cell

I am trying to copy a row from one sheet to another based on the presence of a specific word in the cell. I have one sheet called data, and a second sheet called final.

Here is sample data

A         B            C               D  
john      mary         555.555.4939    initial reply to phone conversation  
Jim       jack         555.555.5555    floor estimate for bathroom  
jerry     kim          555.555.5553    initial response to phone call

I would like to copy the entire row from the "data" sheet to the "final" sheet if the data in column D contains either the word "reply" or the word "response".

+5
source share
1 answer

This should work

Sub Foo()

Dim i As Long, iMatches As Long
Dim aTokens() As String: aTokens = Split("reply,response", ",")

For Each cell In Sheets("data").Range("D:D")

    If (Len(cell.Value) = 0) Then Exit For

    For i = 0 To UBound(aTokens)
        If InStr(1, cell.Value, aTokens(i), vbTextCompare) Then
            iMatches = (iMatches + 1)
            Sheets("data").Rows(cell.Row).Copy Sheets("final").Rows(iMatches)
        End If
    Next

Next

End Sub
+6
source

All Articles