Jenny
Jenny

Reputation: 451

Highlighting the cell, depending on the text

I have an sheet, and in column T, i would like to Highlight the cells, that contain "ok".

I have a code, which works fine with interior.color , but failed to Highlight the cells that contains "OK", instead it is highlighting irrespective of the cell value.

Could anyone suggest, what is wrong with my code ?

Sub colour()
Dim totalrows As Long

totalrows = Sheets("S1").Cells(Rows.Count, "T").End(xlUp).Row
With Range("T5:T" & totalrows)
.Value = "OK"
Range("T5:T" & totalrows).Interior.Color = RGB(0, 255, 0)
End With

End Sub 

Upvotes: 0

Views: 52

Answers (1)

Mrig
Mrig

Reputation: 11727

Try this:

Sub colour()
    Dim totalrows As Long
    Dim cel As Range

    totalrows = Sheets("S1").Cells(Rows.Count, "T").End(xlUp).Row
    For Each cel In Range("T5:T" & totalrows)
        If cel.Value = "OK" Then
            cel.Interior.Color = RGB(0, 255, 0)
        End If
    Next cel
End Sub

Upvotes: 1

Related Questions