Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

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?

like image 304
Christian Avatar asked Dec 15 '11 15:12

Christian


People also ask

What is Isempty in VBA?

VBA IsEmpty is a logical function that tests whether selected is empty or not. Since it is a logical function it will return the results in Boolean values i.e. either TRUE or FALSE. If the selected cell is empty it will return TRUE or else it will return FALSE.

How do you check cell is empty in Excel VBA?

If you wish to test whether a worksheet cell is empty in VBA, you can not use the worksheet function called ISBLANK. In VBA, you must use the ISEMPTY function. In this example, we will test whether cell A1 is empty. If cell A1 is empty, the message "Cell A1 is empty" will be displayed.

Is Empty range VBA?

To check if a cell is empty you can use VBA's ISEMPTY function. In this function, you need to use the range object to specify the cell that you want to check, and it returns true if that cell is empty, otherwise false. You can use a message box or use a cell to get the result.

Is null or empty VBA?

ISNULL in VBA is a logical function which is used to determine whether a given reference is empty or NULL or not that is why the name ISNULL, this is an inbuilt function which gives us true or false as a result, based on the result we can arrive to conclusions, if the reference is empty it returns true value else false ...


2 Answers

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.

like image 129
Justin Self Avatar answered Sep 22 '22 04:09

Justin Self


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

like image 24
iDevlop Avatar answered Sep 21 '22 04:09

iDevlop