Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50331

Macro to search for a string and copy its row to another sheet

$
0
0
Hi Guys,

I have a macro to search for a string and copy its row to another sheet but it does not search through all the columns as it leaves some cells with search string without copying its rows. Please see below and attached.

Code:

Dim myWord$
myWord = InputBox("What key word to copy rows", "Enter your word")
If myWord = "" Then Exit Sub
 
Application.ScreenUpdating = False
Dim xRow&, NextRow&, LastRow&
NextRow = 2
LastRow = Cells.Find(What:="*", After:=Range("L1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Column

For xRow = 1 To LastRow
If WorksheetFunction.CountIf(Rows(xRow), "*" & myWord & "*") > 0 Then
    Rows(x[U]Row).Copy Sheets("Search Result").Rows(NextRow)
NextRow = NextRow + 1
End If
Next xRow
Application.ScreenUpdating = True
 
MsgBox "Macro is complete, " & NextRow - 2 & " rows containing" & vbCrLf & _
"''" & myWord & "''" & " were copied to Search Result Sheet.", 64, "Done"
End Sub


Viewing all articles
Browse latest Browse all 50331

Latest Images

Trending Articles



Latest Images