I'm new to VBA and I'm trying to make a macro that searches through column C finds all the cells containing "teston" then finds the cell below it containing "testoff" and highlights all of the cells in between them in the column next to it. there are multiple instances of teston to testoff.
this code works but only highlights the first instance of teston to testoff
Dim findrow As Long, findrow2 As Long
On Error GoTo errhandler
findrow = Range("C:C").Find("teston", Range("C1")).Row
findrow2 = Range("C:C").Find("testoff", Range("C" & findrow)).Row
Range("F" & findrow + 1 & ":F" & findrow2 - 1).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 16764159
.TintAndShade = 0
.PatternTintAndShade = 0
End With
errhandler:
MsgBox "No Cells containing specified text found"
This is what i tried to do to highlight them all but it doesn't highlight anything
Range("A1").Select
Selection.End(xlDown).Select
Dim lastcell As Long
lastcell = ActiveCell.Row
Dim findrow As Long, findrow2 As Long, I As Long, inext As Long
inext = 1
On Error GoTo errhandler
Do While I < lastcell
findrow = Range("C" & inext & ":" & "C" & lastcell).Find("test1", Range("C1")).Row
findrow2 = Range("C" & inext & ":" & "C" & lastcell).Find("test2", Range("C" & findrow)).Row
Range("F" & findrow + 1 & ":F" & findrow2 - 1).Select
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 16764159
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Range("findrow2").Select
inext = ActiveCell.Row
findrow = findrow2
I = I + 1
Loop
errhandler:
MsgBox "No Cells containing specified text found"