how do I make a VBA to search a few times with different highlight color?
For example, the first time I search is "AS", then all results with "AS" will be highlighted in yellow.
Then the second time, I search is "BD", then all results with "BD" will be highlighted in green.
Then the 3rd, I search is "NE", then all results with "NE" will be highlighted in red.
.....
.....
.....
Until 8 times, then all highlight will be cleared/removed after that. And a perfect loop start, the first search of anything will be highlighted in yellow.
and
then how do i make the highlight area, not only in the exact cell but also the 3 cells next to it?
The below is my code. Please advice.
For example, the first time I search is "AS", then all results with "AS" will be highlighted in yellow.
Then the second time, I search is "BD", then all results with "BD" will be highlighted in green.
Then the 3rd, I search is "NE", then all results with "NE" will be highlighted in red.
.....
.....
.....
Until 8 times, then all highlight will be cleared/removed after that. And a perfect loop start, the first search of anything will be highlighted in yellow.
and
then how do i make the highlight area, not only in the exact cell but also the 3 cells next to it?
The below is my code. Please advice.
VBA Code:
Sub FindRange()
Dim xRg As Range
Dim xFRg As Range
Dim xStrAddress As String
Dim xVrt As Variant
Dim xRow As Long
xVrt = Application.InputBox(prompt:="Search:", Title:="SearchKeyword")
If xVrt <> "" Then
Set xFRg = ActiveSheet.Cells.Find(what:=xVrt)
If xFRg Is Nothing Then
MsgBox prompt:="Cannot find this value", Title:="SearchKeyword"
Exit Sub
End If
xStrAddress = xFRg.Address
Set xRg = xFRg
Do
Set xFRg = ActiveSheet.Cells.FindNext(After:=xFRg)
Set xRg = Application.Union(xRg, xFRg)
Loop Until xFRg.Address = xStrAddress
If xRg.Count > 0 Then
xRg.EntireRow.Interior.ColorIndex = 8
End If
End If
End Sub
Last edited by a moderator: