简体   繁体   English

VBA(Excel)中= Empty和IsEmpty()之间有什么区别?

[英]What is the difference between =Empty and IsEmpty() in VBA (Excel)?

I have used the following VBA code: 我使用了以下VBA代码:

Do While .Cell(i,1) <> Empty
  ' doing things
  i = i+1
Loop

to iterate through columns (with Double/Integer values) in Excel. 在Excel中迭代列(具有Double / Integer值)。 Then I found a case where the test evaluates to False whenever the value of the cell is 0. I have no idea what the difference is between this case and the working ones. 然后我发现了一个案例,只要单元格的值为0,测试就会计算为False。我不知道这个案例和工作案例之间有什么区别。

If I change the test to: 如果我将测试更改为:

Do While Not IsEmpty(.Cell(i,1))
  ..
Loop

it works fine. 它工作正常。 So my question is: What is the difference between how IsEmpty() and =Empty is evaluated? 所以我的问题是:如何评估IsEmpty()和= Empty之间的区别? In what cases will =Empty evaluate to True for cells with value 0? 对于值为0的单元格,在什么情况下将= Empty计算为True?

Empty refers to a variable being at its default value. Empty表示变量处于其默认值。 So if you check if a cell with a value of 0 = Empty then it would return true. 因此,如果检查值为0的单元格是否为Empty则返回true。

IsEmpty refers to no value being initialized. IsEmpty指的是没有初始化的值。

In a nutshell, if you want to see if a cell is empty (as in nothing exists in its value) then use IsEmpty . 简而言之,如果您想查看单元格是否为空(如其值中没有任何内容),请使用IsEmpty If you want to see if something is currently in its default value then use Empty . 如果要查看某些内容当前是否为默认值,请使用Empty

From the Help: 来自帮助:
IsEmpty returns True if the variable is uninitialized, or is explicitly set to Empty ; 如果变量未初始化,或者显式设置为Empty ,则IsEmpty返回True; otherwise, it returns False. 否则,返回False。 False is always returned if expression contains more than one variable. 如果expression包含多个变量,则始终返回False。
IsEmpty only returns meaningful information for variants . IsEmpty 仅返回变体的有意义信息

To check if a cell is empty, you can use cell(x,y) = "" . 要检查单元格是否为空,可以使用cell(x,y) = ""
You might eventually save time by using Range("X:Y").SpecialCells(xlCellTypeBlanks) or xlCellTypeConstants or xlCellTypeFormulas 您最终可以通过使用Range("X:Y").SpecialCells(xlCellTypeBlanks)来节省时间Range("X:Y").SpecialCells(xlCellTypeBlanks)xlCellTypeConstantsxlCellTypeFormulas

我相信IsEmpty只是获取Cell的返回值并检查其是否为空的方法:IsEmpty(.Cell(i,1))是否 - >

return .Cell(i,1) <> Empty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM