Hi, I need to loop thru a range in Active sheet, (Column E from first to last row) and find one of many different possible words. If a word is found, the hole row will be copied to Sheet2.
So far no problems.
However, I need to check each row for more than 100 different words, and the list of words is dynamic. Words will be deleted, and other added. One way to do this is to read the words into an array every time the code is running, and then loop thru that array for each row.
It will rarely be more than 500 rows, but with 100-150 words to check for it will take a little time. So far I have just been searching for 15-20 words, all static, but the needs have changed.
Does anyone has any suggestion to the best way of doing this?
I have up to now used the code below, but it has become impractical now.
So far no problems.
However, I need to check each row for more than 100 different words, and the list of words is dynamic. Words will be deleted, and other added. One way to do this is to read the words into an array every time the code is running, and then loop thru that array for each row.
It will rarely be more than 500 rows, but with 100-150 words to check for it will take a little time. So far I have just been searching for 15-20 words, all static, but the needs have changed.
Does anyone has any suggestion to the best way of doing this?
I have up to now used the code below, but it has become impractical now.
VBA Code:
Sub Find_and_copy ()
Dim sh1 As Worksheet, sh2 As Worksheet, rng As Range, cel As Range
Dim rngCopy As Range, lastR1 As Long, lastR2 As Long
Dim strSearch1 As String, strSearch2 As String
strSearch1 = "teststring1"
strSearch2 = "teststring2"
Set sh1 = ActiveSheet
Set sh2 = Worksheets("Sheet2")
lastR1 = sh1.Range("C" & Rows.count).End(xlUp).Row
lastR2 = sh2.Range("A" & Rows.count).End(xlUp).Row + 1
Set rng = sh1.Range("C2:C" & lastR1)
For Each cel In rng.cells
If cel.Value = strSearch Or cel.Value = strSearch2 Then
If rngCopy Is Nothing Then
Set rngCopy = sh1.Rows(cel.Row)
Else
Set rngCopy = Union(rngCopy, sh1.Rows(cel.Row))
End If
End If
Next
If Not rngCopy Is Nothing Then
rngCopy.Copy Destination:=sh2.cells(lastR2, 1)
End If
End Sub