Search code examples
vbaexcel

vba are empty values treated as zeroes in equality comparison


I am trying to skip records which either have a zero as the value or is empty. As of now, i have the following code that checks for both explicitly, but I feel that the second check is redundant. But I want to confirm that I am right so that I can remove the second part of the IF

IF (CellInValue(RowInCrnt, ColInCrnt) = 0 Or CellInValue(RowInCrnt, ColInCrnt) = "") Then

Solution

  • No, "" is not equal to 0 and will produce a type mismatch in a strongly typed scenario, or won't come out as equal if used as Variant.

    When the cell is empty, it's also not the same as zero, but it will come out as same, because in VB, Empty = 0 gives True because of implicit conversion happening in the background.
    Same for Empty = "", will also give True for the same reason.

    So you need both checks.