S
StillLearning
With the help of others, I have the following macro that allows me to take a
list of words, and find them in worksheet. The problem I am having is when
the search comes to a word that it does not find, it errors. Any help you
can provide would be appreciated.
Sub FindValues()
Dim myC As Range
Dim myD As Range
Dim myR As Range
Dim myL As Range
Dim myFindString As String
Dim firstAddress As String
Set myL = Worksheets("Search").Range("A2")
Set myL = Range(myL, myL.End(xlDown))
'Worksheet "Search" and begining cell "A2" ID's words to search for.
For Each myR In myL
myFindString = myR.Value
With Worksheets("data").Cells
'Worksheet "data" to be searched.
Set myC = .Find(myFindString, LookIn:=xlValues, lookAt:=xlPart)
If Not myC Is Nothing Then
Set myD = myC
firstAddress = myC.Address
End If
Set myC = .FindNext(myC)
If Not myC Is Nothing And myC.Address <> firstAddress Then
Do
Set myD = Union(myD, myC)
Set myC = .FindNext(myC)
Loop While Not myC Is Nothing And myC.Address <> firstAddress
End If
End With
'Format statement
myD.Interior.ColorIndex = 3
Set myC = Nothing
Set myD = Nothing
Next myR
End Sub
list of words, and find them in worksheet. The problem I am having is when
the search comes to a word that it does not find, it errors. Any help you
can provide would be appreciated.
Sub FindValues()
Dim myC As Range
Dim myD As Range
Dim myR As Range
Dim myL As Range
Dim myFindString As String
Dim firstAddress As String
Set myL = Worksheets("Search").Range("A2")
Set myL = Range(myL, myL.End(xlDown))
'Worksheet "Search" and begining cell "A2" ID's words to search for.
For Each myR In myL
myFindString = myR.Value
With Worksheets("data").Cells
'Worksheet "data" to be searched.
Set myC = .Find(myFindString, LookIn:=xlValues, lookAt:=xlPart)
If Not myC Is Nothing Then
Set myD = myC
firstAddress = myC.Address
End If
Set myC = .FindNext(myC)
If Not myC Is Nothing And myC.Address <> firstAddress Then
Do
Set myD = Union(myD, myC)
Set myC = .FindNext(myC)
Loop While Not myC Is Nothing And myC.Address <> firstAddress
End If
End With
'Format statement
myD.Interior.ColorIndex = 3
Set myC = Nothing
Set myD = Nothing
Next myR
End Sub