Why 0 is considered 'Empty' in VBA?

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
4,983
Office Version
  1. 365
Platform
  1. Windows
Why 0 is considered 'Empty' in VBA?
I ran this macro:
VBA Code:
Sub test1()
Dim x As Variant
x = 0
Debug.Print IsEmpty(x)  'returns  False
Debug.Print x = Empty   'returns  True
End Sub

Using IsEmpty(x) returns 'False' as I expected, but why x = Empty returns 'True'?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Because of implicit coercion. "" = Empty will also return True as will Empty = False
 
Upvote 0
As soon as you use x = Empty

You would also find that 1 = "1" was true.
 
Upvote 0
As soon as you use x = Empty
Okay, here's my understanding: because x is 0, it is coerced to False, and 'Empty' is also coerced to False. Therefore, False = False, which returns True.
Is my understanding correct?"
 
Upvote 0
Cristian Buse explains this stuff in detail here

Pay special attention to the following section:

Comparing a Variant with Empty is like comparing 2 Variants. When comparing 2 Variants, there are some special rules that apply. Stated here:

If expression1 and expression2 are both Variant expressions, their underlying type determines how they are compared. The following table shows how the expressions are compared or the result from the comparison, depending on the underlying type of the Variant.
IfThen
Both Variant expressions are numericPerform a numeric comparison.
Both Variant expressions are stringsPerform a string comparison.
One Variant expression is numeric and the other is a stringThe numeric expression is less than the string expression.
One Variant expression is Empty and the other is numericPerform a numeric comparison, using 0 as the Empty expression.
One Variant expression is Empty and the other is a stringPerform a string comparison, using a zero-length string ("") as the Empty expression.
Both Variant expressions are EmptyThe expressions are equal.
 
Upvote 0

Forum statistics

Threads
1,215,218
Messages
6,123,676
Members
449,116
Latest member
HypnoFant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top