Article ID: 124924
Article Last Modified on 10/11/2006
Sub FindStrings()
Dim firstCell, nextCell, stringToFind As String
' Show an input box and return the entry to a variable.
stringToFind = _
Application.InputBox("String to find?", "Search String")
' Set an object variable to evaluate the Find command.
Set firstCell = Cells.Find(what:=stringToFind, lookat:=xlWhole, _
searchdirection:=xlPrevious)
' If the string is not found, show this message box.
If firstCell Is Nothing Then
MsgBox "Search Value Not Found.", vbExclamation
Else
' Otherwise, find the next occurrence of the search text.
nextCell = _
Cells.FindNext(after:=Range(firstCell.Address)).Address
' Show its address in a message box.
MsgBox nextCell
' Continue finding the next occurrence as long as the address of
' the found cell is not the same as the first cell.
Do While firstCell.Address <> nextCell
nextCell = Cells.FindNext(after:=Range(nextCell)).Address
MsgBox nextCell
Loop
End If
End Sub
If the range in which the search text is to be found is in a small portion
of the worksheet, use the For...Each control structure instead of the Find
method (For...Each requires less code). The advantage to the Find method
is
108892 XL: Cells.Find Returns Error When No Match Found
Additional query words: 8.00 XL97 XL98 XL7 XL5 FindNext Find Searching
Keywords: kbdtacode kbhowto kbprogramming KB124924