vba 是在等式比较中被视为零的空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13173575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vba are empty values treated as zeroes in equality comparison
提问by Siraj Samsudeen
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 的第二部分
IF (CellInValue(RowInCrnt, ColInCrnt) = 0 Or CellInValue(RowInCrnt, ColInCrnt) = "") Then
回答by GSerg
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
.
不,""
is not equal to0
并且在强类型场景中会产生类型不匹配,或者如果用作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.
当单元格为空时,它也不与零相同,但它会以相同的方式出现,因为在 VB 中,由于在后台发生隐式转换而Empty = 0
给出True
。
同样为Empty = ""
,也会True
出于同样的原因给予。
So you need both checks.
所以你需要两个检查。
回答by Daniel
GSerg is completely correct.
GSerg 是完全正确的。
If you really wanted to avoid doing 2 different checks, you could change your If to something like this:
如果您真的想避免进行 2 次不同的检查,您可以将 If 更改为如下所示:
If Clng(0 & CellInValue(RowInCrnt, ColInCrnt)) = 0 then
This returns true when CellInValue(RowInCrnt, ColInCrnt)
is "", Empty, or 0.
当CellInValue(RowInCrnt, ColInCrnt)
为 ""、Empty 或 0时返回 true 。
Though if your function could return letters instead of numbers, this will have a type mismatch error in that case.
尽管如果您的函数可以返回字母而不是数字,则在这种情况下会出现类型不匹配错误。