I am trying to trace through a sheet and highlight cells that come back as blank values or 0 values (or "NaN") values. I wrote a nested for loop to do this for me, however, I am seeing a weird item.
The cell in question should change to red if the value equals 0. When I run my macro, however, the end result is that all the cells that pass the if statement conditions end as yellow, even if 0. Perhaps VBA reads blank cells as 0, or the cells are not truly blank? Have I made a mistake, or am I incorrect on the logic of VBA?
'i is for the columns
For i = 2 To 4 'lastColumn
totalCounter = 0
outageCounter = 0
missingCounter = 0
'j is for the rows
For j = 6 To lastRow
'highlight if production outage
If mainSheet.Cells(j, i).Value = 0 Then
mainSheet.Cells(j, i).Interior.Color = vbRed
outageCounter = outageCounter + 1
End If
'highlight if comm outage
If mainSheet.Cells(j, i).Value = "" Or mainSheet.Cells(j, i).Value = "NaN" Then
mainSheet.Cells(j, i).Interior.Color = vbYellow
missingCounter = missingCounter + 1
End If
totalCounter = totalCounter + 1
Next j
mainSheet.Cells(lastRow + 2, i).Value = missingCounter
mainSheet.Cells(lastRow + 3, i).Value = outageCounter
mainSheet.Cells(lastRow + 4, i).Value = totalCounter
IFwill be true even when the value in the cell is blank. - vknowles"", and cells containing"NaN"to yellow (after first changing empty cells to red for a very short time). But there is no reason why your code should be changing cells containing0to be yellow as you say in your question ("all the cells that pass the if statement conditions end as yellow, even if 0"). Are you 100% certain that cells containing a0end up as yellow?!? - YowE3K.Valuewill match either 0 or "" in theIfstatements, so, yes, both conditions will be satisfied. - vknowles0ends up yellow - just why an empty cell ends up yellow (after being red for a short time). - YowE3K