You are here:

Excel/Macro to loop a find and highlight text

Advertisement


Question
Hello!

I am trying to figure out how to alter this macro to make it work correctly. Currently I have:

Sub Search()
Range("E4").Select
For iRow = 2 To Range("E65536").End(xlUp).Row

   Cells.Find(What:="fast food", After:=ActiveCell, LookIn:=xlFormulas, _
       LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
       MatchCase:=False, SearchFormat:=False).Activate
   With Selection.Interior
       .Pattern = xlSolid
       .PatternColorIndex = xlAutomatic
       .Color = 65535
       .TintAndShade = 0
       .PatternTintAndShade = 0
   End With

Next

End Sub

I know something is wrong with it, because as soon as it hits the bottom of the data, it returns to the top and continues on an endless loop.

What I am trying to do:
I need the macro to search the spreadsheet for a term, highlight the cell, and then keep going until the last data-filled row, then stop. I need to do this for multiple search terms. The terms can all be put in one macro, correct? Do you see what we did wrong in our current macro?

Thank you very much,
Monja

Answer

You didn't do anything necessarily wrong - you just told the macro to do the search 65534 times. It should stop after that.

If you want it to be a little more dynamic you would do something like

Sub Search()
Dim r As Range, sAddr As String
Range("E4").Select

 Set r = Cells.Find(What:="fast food", _
         After:=Range("E4"), _
         LookIn:=xlFormulas, _
         LookAt:=xlPart, _
         SearchOrder:=xlByRows, _
         SearchDirection:=xlNext, _
         MatchCase:=False, _
         SearchFormat:=False)
 If Not r Is Nothing Then
   sAddr = r.Address
   Do
     With r.Interior
       .Pattern = xlSolid
       .PatternColorIndex = xlAutomatic
       .Color = 65535
       .TintAndShade = 0
       .PatternTintAndShade = 0
     End With
     Set r = Cells.FindNext(r)
   Loop While Not r Is Nothing And r.Address <> sAddr
 End If


End Sub


If you want to process a list of strings to search for then you could do it this way.


Sub SearchMany()
Dim r As Range, sAddr As String
Dim v As Variant, i As Long

v = Array("fast food", "dog", "cat", "milk", "cookies")
For i = LBound(v) To UBound(v)
 Set r = Nothing
 sAddr = ""
 Range("E4").Select
 Set r = Cells.Find(What:=v(i), After:=Range("E4"), LookIn:=xlFormulas, _
       LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
       MatchCase:=False, SearchFormat:=False)
 If Not r Is Nothing Then
   sAddr = r.Address
   Do
     With r.Interior
       .Pattern = xlSolid
       .PatternColorIndex = xlAutomatic
       .Color = 65535
       .TintAndShade = 0
       .PatternTintAndShade = 0
     End With
     Set r = Cells.FindNext(r)
   Loop While Not r Is Nothing And r.Address <> sAddr
 End If
Next i

End Sub

Both procedures were tested and worked for me.

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.