LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Text search only finds 1st result

Thanks for taking the time to responsed Dave. It is amazing how many
different ways you can achieve the same thing in Excel.

"Dave Peterson" wrote:

You write worksheet (most people mean a single worksheet), but then you loop
through all the worksheets in the active workbook.

I'm gonna guess that you really wanted to loop through all the worksheets in
that workbook and find that string:

Option Explicit
Private Sub CommandButton1_Click()

Dim Wks As Worksheet
Dim myWord As String
Dim FoundCell As Range
Dim FirstAddress As String

myWord = InputBox("Please enter a Word")
If Trim(myWord) = "" Then
Exit Sub 'user hit cancel
End If

For Each Wks In Worksheets
Set FoundCell = Nothing
FirstAddress = ""
With Wks
'do the entire usedrange at once.
.UsedRange.Interior.ColorIndex = xlNone

Set FoundCell = .Cells.Find(what:=myWord, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)

If FoundCell Is Nothing Then
'not on this sheet
Else
FirstAddress = FoundCell.Address

Do
'do the work
With FoundCell.Interior
.ColorIndex = 6
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With

'look for more
Set FoundCell = .Cells.FindNext(after:=FoundCell)

If FoundCell Is Nothing Then
'this shouldn't happen
Exit Do
End If

If FoundCell.Address = FirstAddress Then
'back to the first cell, so exit the loop
Exit Do
End If
Loop
End If
End With
Next Wks
End Sub

Felixdecat wrote:

Hello, I have written the following code. The purpose is to find every
instance of a word in the worksheet, and then highlight it yellow. However,
it only finds the 1st instance, but does not carry on searching the rest of
the worksheet. Can anyone help me? The first part of the code clears the
previous search results.

Private Sub CommandButton1_Click()

Dim Wsht As Worksheet
Dim Rng As Range
Dim cl As Range
For Each Wsht In Worksheets
Set Rng = Wsht.UsedRange
For Each cl In Rng
With cl
If .Interior.ColorIndex = 6 Then
.Interior.ColorIndex = 0
End If
End With
Next cl
Next Wsht

Set Rng = Nothing
Set cl = Nothing

w = InputBox("Please enter a Word")

Cells.Find(What:=(w), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
End Sub


--

Dave Peterson
.

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Functions (search within search result) reply to this please Nick Excel Worksheet Functions 1 February 17th 09 03:57 AM
Search for Text within a Formula Result Q Sean Excel Worksheet Functions 5 February 14th 08 03:50 PM
to search for a string and affect data if it finds the string? Shwaman Excel Worksheet Functions 1 January 11th 06 12:56 AM
Process a row if a string search finds certain words from a list Reuel Excel Programming 1 December 27th 05 02:02 AM
macro that finds text and keeps only part of it john mcmichael Excel Discussion (Misc queries) 5 October 18th 05 10:58 PM


All times are GMT +1. The time now is 07:48 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"