I got the sub below from: Format cell based on formula value from "JosieP"
I am trying to test for Null cells as the sub fails if it encounters one. If it encounters a null cell I want to add a color to the cell
If IsNull(rCell) Then rCell.Interior.Color = 8
does not work but does not fail either.
if clng(Left(Right(rcell.value, 2), 1)) < 3 Then rcell.Interior.ColorIndex = 10
fails when there is a null cell .
I tried adding Not IsNull(rCell) so I would have
if clng(Left(Right(rcell.value, 2), 1)) < 3 And Not IsNull(rCell) Then rcell.Interior.ColorIndex = 10
but this fails as well.
Sub Format()
Dim LastRow As Long
Dim WS As Worksheet
dim rCell as range
Set WS = Sheets("sheet1")
LastRow = WS.range("F" & WS.Rows.Count).End(xlUp).Row
for each rcell in WS.range("F2:F" & LastRow).cells
If IsNull(rCell) Then rCell.Interior.Color = 8
if clng(Left(Right(rcell.value, 2), 1)) < 3 And Not IsNull(rCell) Then rcell.Interior.ColorIndex = 10
next rcell
End Sub
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…