Empty

This keyword/value can only be used in conjunction with the Variant data type.
This keyword/value is used when a variable declared as Variant has not been assigned a value.
A variant variable has the Empty value prior to it being assigned a value.


Initialising a Variant

The Empty data type is the default value when you declare a variable as a Variant.

Dim vMyVariant As Variant 
If (vMyVariant = Empty) Then
End If

Empty = 0

The Empty keyword is also a zero value.
A variant containing Empty is 0 when used in a numerical context.

Dim vMyVariant As Variant 
If (vMyVariant = 0) Then
   Call MsgBox("zero value")
End If

Empty = ""

The Empty keyword is also a zero length string
A variant containing Empty is a zero length string ("") when used in a string context.

Dim vMyVariant As Variant 
If (vMyVariant = "") Then
   Call MsgBox("empty string value")
End If

Assigning Empty

The Empty data type can be assigned explicitly to a Variant data type to indicate that a variable does not contain a value.

Dim vMyVariant As Variant 
vMyVariant = "sometext"
vMyVariant = Empty

IsEmpty Function

The ISEMPTY returns True or False indicating if a variable has been initialised

Dim vMyVariant As Variant 
If (IsEmpty(vMyVariant) = True) Then
   Call MsgBox("empty value")
End If

vbVarType.vbEmpty

If you assign a variable to vbEmpty then you are actually just assigning the numerical value zero.

Dim vMyVariant As Variant 
vMyVariant = "some text"
vMyVariant = VBA.vbVarType.vbEmpty
If (IsEmpty(vMyVariant) = True) Then
'This is False because the myVariant variable has been assigned the value 0.
End If

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext