The following routine is a very rough expansion of the FIND example in Excel
97's VB
help file that will activate each sheet in the current workbook in turn &
change a cell to red if it contains "Test Phrase". (Obviously, one would
need to customise the phrase being searched for as well as the action within
the Do...While loop.)
One caveat: it will intentionally skip over any worksheet where the
UsedRange consists of only a single cell or the sheet is otherwise empty. (I
was being lazy at the time I wrote it & didn't feel like trapping the error
generated in those very rare instances).
The syntax for Replace is similar to Find, but not identical, & could be
adapted.
Sub SearchWorkbook()
Dim wksht As Worksheet
Dim c As Range
Dim firstaddress As String
For Each wksht In Sheets
wksht.Activate
With ActiveSheet.UsedRange
If .Count > 1 Then
' Act only if worksheet has more than one cell in use
Set c = .Find(What:="Test Phrase", _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not c Is Nothing Then
firstaddress = c.Address
Do
' Customize per your needs
c.Interior.ColorIndex = 3
Set c = .FindNext(c)
Loop While Not c Is Nothing And c.Address <>
firstaddress
End If
End If
End With
Next wksht
End Sub
Good Luck,
George
>I thought I was an "expert", but this one has me stumped (and I am sure
>the solution is simple).
>I want to search all the worksheets in a workbook to find a word or
>phrase of text. It is easy enough to search a single sheet but no matter
>how I try to use the FOR EACH command, the FIND command does not seem to
>work.
>Any ideas would be appreciated!
>TIA