Search for text and highlight row

TaskMaster

Board Regular
Joined
Oct 15, 2020
Messages
75
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all I have the following that sequentially searches for FB09 and copies the data to another tab, I would like the row of the data being copied to be highlighted a colour as a check to tell me that data has being picked up. Is there a way to tweak this to do this?

VBA Code:
Sub FindAndCopyM()
    Dim WS1 As Worksheet, WS2 As Worksheet
    Dim R As Range, SearchRange As Range
    Dim S As String, SearchStr As String ,FirstAddr As String
    Dim LastRow As Long
    Dim FirstPass As Boolean

    Set WS1 = Worksheets("data")
    Set WS2 = Worksheets("summary")

    SearchStr = "FB09"

    Set SearchRange = WS1.Range("S1", WS1.Range("S" & WS1.Rows.Count).End(xlUp))

    With SearchRange
        Set R = .Find(what:=SearchStr, after:=.Cells(.Cells.Count), lookat:=xlPart, MatchCase:=True, searchdirection:=xlNext)
    End With

    If Not R Is Nothing Then
        FirstAddr = R.Address
        FirstPass = True
    End If

    Do While Not R Is Nothing
        If Not FirstPass Then
            Set R = SearchRange.Find(what:=SearchStr, after:=R, lookat:=xlPart, MatchCase:=True, searchdirection:=xlNext)
        End If

        If Not FirstPass Then
            If R.Address = FirstAddr Then
                Set R = Nothing
                Exit Do
            End If
        Else
            FirstPass = False
        End If
       
        If Not R Is Nothing Then
            If InStr(R.Value, SearchStr) = 1 Then
                S = Right(R.Value, 8)
                If WS2.Range("A23").Value = "" Then
                    WS2.Range("A23").NumberFormat = "@"
                    WS2.Range("A23").Value = S
                Else
                    With WS2
                        LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
                         .Range("A" & LastRow + 1).NumberFormat = "@"
                        .Range("A" & LastRow + 1).Value = S
                    End With
                End If
            End If
        End If
    Loop
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
First and third line is in your existing code, add the one in between.

VBA Code:
If InStr(R.Value, SearchStr) = 1 Then
                R.Interior.Color = 65535
                S = Right(R.Value, 8)
 
Upvote 0
First and third line is in your existing code, add the one in between.

VBA Code:
If InStr(R.Value, SearchStr) = 1 Then
                R.Interior.Color = 65535
                S = Right(R.Value, 8)
Hi thank you for your reply, this works brilliantly, however would it be possible for it to highlight the whole row, columns A:AA?
 
Upvote 0
Just change this line:
VBA Code:
R.Interior.Color = 65535
to this:
VBA Code:
Range(Cells(R.Row, "A"),Cells(R.Row, "AA")).Interior.Color = 65535
 
Upvote 0
Solution
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,159
Members
452,615
Latest member
bogeys2birdies

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