vba Len(cell.Value) = 0 和 cell.Value = "" 在 Excel 中是否等效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4873569/
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
Are Len(cell.Value) = 0 and cell.Value = "" equivalent in Excel?
提问by strcompnice
When evaluating the value of a cell in a worksheet in Excel, is the following statement true?
在 Excel 中评估工作表中单元格的值时,以下语句是否正确?
If and only if Len(cell.Value) = 0
, then cell.Value = "".
当且仅当Len(cell.Value) = 0
,则cell.Value = "".
This would imply that it is safe to check that there are zero characters in a cell instead of comparing the cell value to an empty string.
这意味着检查单元格中是否有零个字符而不是将单元格值与空字符串进行比较是安全的。
回答by codingbadger
Yes, they do the same thing.
是的,他们做同样的事情。
However, I would advise that you use the IsEmpty
function to check for empty cells. If you enter a single quote into a cell '
both the len
check and the =""
check will both state the the cell is empty. When in actual fact it isn't.
但是,我建议您使用该IsEmpty
函数来检查空单元格。如果您在单元格中输入单引号'
,则len
检查和=""
检查都将说明该单元格为空。当事实并非如此。
IsEmpty
will return false in this scenario.
IsEmpty
在这种情况下将返回 false。
And it's also a little easier to read.
而且它也更容易阅读。
回答by Sébastien Nussbaumer
I'm quite sure your statement is true. I've been using it for quite a while and never had an issue with that assumption
我很确定你的说法是正确的。我已经使用它很长一段时间了,从来没有遇到过这个假设的问题
My two cents : I'm not sure you gain a lot of performance from the trick, and it maybe easier for other people to understand the meaning of cell.Value = ""
than Len(cell.Value) = 0
我的两分钱:我不确定你是否从这个技巧中获得了很多性能,而且其他人可能更容易理解cell.Value = ""
比的含义Len(cell.Value) = 0