简体   繁体   中英

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

I have used the following VBA code:

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

to iterate through columns (with Double/Integer values) in Excel. 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.

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? In what cases will =Empty evaluate to True for cells with value 0?

Empty refers to a variable being at its default value. So if you check if a cell with a value of 0 = Empty then it would return true.

IsEmpty refers to no value being initialized.

In a nutshell, if you want to see if a cell is empty (as in nothing exists in its value) then use IsEmpty . If you want to see if something is currently in its default value then use Empty .

From the Help:
IsEmpty returns True if the variable is uninitialized, or is explicitly set to Empty ; otherwise, it returns False. False is always returned if expression contains more than one variable.
IsEmpty only returns meaningful information for variants .

To check if a cell is empty, you can use cell(x,y) = "" .
You might eventually save time by using Range("X:Y").SpecialCells(xlCellTypeBlanks) or xlCellTypeConstants or xlCellTypeFormulas

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

return .Cell(i,1) <> Empty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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