An Article About Nothing
There are at least seven identifiers that convey nothingness in VBA:
Nothing
Null
""
vbNullString
vbNullChar
Empty
Missing
Nothing
Nothing
is the starting value of an object variable before it has been initialized (or after it has been explicitly set to Nothing).
Null
Null is a value that generally means "unknown." In VBA, only Variant
data types can hold a value of Null
. We use the IsNull()
function to test if a variable contains a null value.
""
Two double quotes on their own are used to represent a zero-length string.
vbNullString
The built-in vbNullString
constant is similar to ""
. The two can be used interchangeably in most situations.
The key difference is that ""
is a real string (meaning it occupies storage in memory) whereas vbNullString
is a "null string pointer." VBA treats it like a string, but it points to a memory address of zero (meaning it does not exist in memory).
vbNullChar
Character with an ASCII code of zero. Equivalent to Chr(0)
.
This constant is most often used when interacting with C/C++ via API calls, as C-style strings use the null character to indicate the end of a string.
Empty
This is the starting value of a Variant
variable before it has been assigned a value. Use the function IsEmpty()
to check for this value.
Missing
This is the value of an Optional
Variant
parameter when the calling code does not assign a value to the parameter. Use the function IsMissing()
to check for this value.
This article inspired by A Show About Nothing:
Cover image created with Microsoft Designer
UPDATE [2023-08-11]: Added brief mentions of the IsEmpty()
and IsMissing()
functions that are used to check for Empty
and Missing
values, respectively (h/t Colin Riddington).
UPDATE [2023-09-06]: Added links to deep dives on the following topics: Nothing, Null, Empty, and Missing.
UPDATE [2023-11-21]: Added links to deep dives on the following topic: vbNullString and vbNullChar.