Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vba are empty values treated as zeroes in equality comparison

Tags:

excel

vba

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
like image 553
Siraj Samsudeen Avatar asked Dec 20 '22 14:12

Siraj Samsudeen


2 Answers

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.

like image 93
GSerg Avatar answered Mar 05 '23 05:03

GSerg


GSerg is completely correct.

If you really wanted to avoid doing 2 different checks, you could change your If to something like this:

If Clng(0 & CellInValue(RowInCrnt, ColInCrnt)) = 0 then

This returns true when CellInValue(RowInCrnt, ColInCrnt) is "", Empty, or 0.

Though if your function could return letters instead of numbers, this will have a type mismatch error in that case.

like image 30
Daniel Avatar answered Mar 05 '23 04:03

Daniel