FIND & HIGHLIGHT MULTIPLE CELLS (SOLVED)

BrianB

Well-known Member
Joined
Feb 17, 2003
Messages
8,127
Questions based on the need to find all data in a worksheet that matches the search key, and then doing something often arise. Here is the basic code structure.
Code:
'=====================================================
'- FIND & HIGHLIGHT CELLS DEPENDING ON CONTENT
'- Brian Baulsom January 2005
'=====================================================
Sub FindHiLight()
    Dim MyFind As Variant
    Dim MyNewValue As Variant
    Dim FoundCell As Object
    Dim Counter As Long
    '-------------------------------------------------
    '- SET SEARCH KEY
    MyFind = InputBox("Please insert value to find.")
    If MyFind = "" Then End
    Counter = 0
    '------------------------------------------------
    '- FIND ALL MATCHING CELLS
    On Error Resume Next
    Set ws = ActiveSheet
    Set FoundCell = ws.Cells.Find(what:=MyFind)
    If Not FoundCell Is Nothing Then
        FirstAddress = FoundCell.Address
        Do
            Counter = Counter + 1
            '--------------------------------------------
            '- what to do if found
            FoundCell.Interior.ColorIndex = 16
            '--------------------------------------------
            Set FoundCell = ws.Cells.FindNext(FoundCell)
        Loop While Not FoundCell Is Nothing _
            And FoundCell.Address <> FirstAddress
    End If
    rsp = MsgBox("Found " & Counter)
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,215,461
Messages
6,124,958
Members
449,200
Latest member
indiansth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top