The direct answer: in VBA, use the IsNull function to test whether a Variant variable or expression contains the special Null value (e.g., If IsNull(myVar) Then). For object variables, test for Nothing with If myObject Is Nothing Then, and for uninitialized or empty Variants, use IsEmpty. These three tests are distinct and not interchangeable.
Background: Why “Null” Is Confusing in VBA
VBA has several “nothing-like” states, and each one means something different. Null is a special value that indicates a variable contains no valid data — it is not an empty string (""), not zero, and not an uninitialized variable. Null comes from database programming (e.g., Access, SQL Server) and represents a missing or unknown value in a field. In Excel, a cell that is truly empty returns Empty when read into a Variant, not Null. A cell containing the text “NULL” is just a string, not the Null value. Many beginners mistakenly compare a variable to Nothing or use = Null, which never works as expected because Null propagates through expressions — any operation involving Null returns Null.
How to Test for Null: The IsNull Function
Use IsNull(expression) to check if an expression evaluates to Null. The function returns True if the expression is Null, otherwise False. Here is a simple example:
Dim v As Variant
v = Null
If IsNull(v) Then
Debug.Print "v is Null"
End If
Note that IsNull only works on Variant variables or expressions that can hold Null. If you pass a non-Variant variable (like a String or Integer), it will always return False because those types cannot contain Null. For example, Dim s As String: s = Null will raise an error — you cannot assign Null to a String variable. So always declare variables that might hold Null as Variant.
Testing for Nothing (Object Variables)
When you work with object variables (e.g., Worksheet, Range, Collection), the “no object” state is Nothing, not Null. Test with Is Nothing:
Dim ws As Worksheet
Set ws = Nothing
If ws Is Nothing Then
Debug.Print "ws is Nothing"
End If
This is a common source of confusion: IsNull(ws) will always return False because ws is a reference, not a Variant. Also, never use ws = Nothing — that will raise a “Type mismatch” error because Nothing is not a value you can compare with =; you must use Is.
Testing for Empty (Uninitialized Variants)
An uninitialized Variant has the value Empty. This is different from Null and from an empty string. Use IsEmpty to test:
Dim v As Variant
' v is automatically Empty
If IsEmpty(v) Then
Debug.Print "v is Empty"
End If
In Excel, reading an empty cell into a Variant gives Empty, not Null. So if you loop through cells and want to skip truly empty cells, check IsEmpty(cell.Value). But be careful: a cell containing a formula that returns “” is not empty — it contains a zero-length string. IsEmpty will return False for that cell.
Common Problems and Misconceptions
- Using
= Null: In VBA,If myVar = Null Thenwill never beTruebecause any comparison withNullyieldsNull, which is treated asFalsein anIfstatement. Always useIsNull. - Confusing
NullwithNothing:Nullis a Variant value;Nothingis an object reference state. They are not interchangeable. - Confusing
NullwithEmpty:Emptymeans the variable has never been assigned;Nullmeans it was explicitly set to no valid data. In databases, a missing field isNull, notEmpty. - Testing a non-Variant: If you declare
Dim x As Integer,IsNull(x)always returnsFalsebecause an Integer can never beNull. Only Variants can holdNull. - Null propagation: If you concatenate strings with
&, aNulloperand results inNull, not an empty string. UseNz()(in Access) orIIf(IsNull(x), "", x)to handle it.
How to Choose the Right Test
Follow this decision guide:
- Is the variable declared as
Variant? Then useIsNullto check forNull,IsEmptyto check for uninitialized, andLen(v) = 0to check for an empty string (but noteLen(Null)raises an error, so checkIsNullfirst). - Is the variable an object reference? Use
Is Nothing. - Is the variable a fixed-type (String, Integer, etc.)? It can never be
NullorNothing— only its default value (e.g., 0 for numbers, “” for strings) matters.
For a robust check on a Variant that might be Null, Empty, or a string, use a nested condition:
If IsNull(v) Then
' handle Null
ElseIf IsEmpty(v) Then
' handle Empty
ElseIf v = "" Then
' handle empty string
Else
' handle actual data
End If
Practical Examples in Excel VBA
Here is a common scenario: reading a range and checking for empty cells. The following macro loops through A1:A10 and reports which cells are truly empty (using IsEmpty) versus containing a formula that returns “”.
Sub CheckEmptyCells()
Dim cell As Range
Dim v As Variant
For Each cell In Range("A1:A10")
v = cell.Value
If IsEmpty(v) Then
Debug.Print cell.Address & " is truly empty"
ElseIf v = "" Then
Debug.Print cell.Address & " contains empty string"
Else
Debug.Print cell.Address & " has value: " & v
End If
Next cell
End Sub
Another example: when reading from an Access database, fields can be Null. Use IsNull to avoid errors:
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Table1")
If Not IsNull(rs!Field1) Then
Debug.Print rs!Field1
Else
Debug.Print "Field1 is Null"
End If
Remember to always save a copy of your workbook before running any macro, as macros cannot be undone with Ctrl+Z.
Frequently Asked Questions
Why does IsNull return False for an empty cell?
Because an empty Excel cell has the value Empty, not Null. Empty is a special Variant value indicating the variable has not been initialized. When you read an empty cell into a Variant, VBA assigns Empty. To test for truly empty cells, use IsEmpty, not IsNull.
Can I use IsNull to check if a Range object is set?
No. IsNull works only on Variant values. For object variables like Range, use If myRange Is Nothing Then. For example, Set myRange = Range("A1") sets a reference; if you never set it, it is Nothing.
What is the difference between Null and Nothing in VBA?
Null is a value that a Variant can hold, indicating missing or unknown data (like a NULL in a database). Nothing is a special object reference state meaning the variable does not refer to any object. You test for Null with IsNull and for Nothing with Is Nothing. They are not interchangeable.
How do I check if a cell is empty but contains a formula that returns “”?
Use IsEmpty to check for truly empty cells. If a formula returns “”, the cell is not empty — it contains a zero-length string. To detect that, compare the cell value to "" after checking IsEmpty is False. For example: If Not IsEmpty(cell.Value) And cell.Value = "" Then.
Does IsNull work on arrays?
Yes, but only on the elements. An array itself is not a Variant, but each element can be a Variant. You can test an element with IsNull(arr(0)) if the array is declared as Variant. If the array is of a fixed type (e.g., Integer), elements cannot be Null.
What happens if I use IsNull on a variable that is not a Variant?
It will always return False because non-Variant data types cannot hold Null. For example, Dim i As Integer: Debug.Print IsNull(i) prints False. To store Null, you must declare the variable as Variant.
Related
- Budget Balance JavaScript Lab: How to Build It
- Good Budget Excel Template Free? Yes, Here’s How
- Data Entry Roster Management: How It Works
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Convert to String: How CStr WorksIn VBA, use the built-in CStr function to convert a value to a string: for example, CStr(42) returns…
- Autofit in Excel Using VBA for Rows and ColumnsTo autofit Excel cells with code, run VBA that applies AutoFit to the used range: use ws.UsedRange.Columns.AutoFit for…
- Excel VBA: Copy and Paste Range ValuesTo copy a range’s results without its formulas or formatting, assign the source range’s Value property to a…
- Excel VBA Lowercase: Use LCase in Macros and FunctionsIn Excel VBA, use the built-in LCase function to convert text to lowercase; its string-returning counterpart, LCase$, is…
- Excel VBA InputBox Type: Values, Examples, and UseIn Excel VBA, the Type argument belongs to Application.InputBox and controls what kind of response the dialog accepts:…
- VBA AutoFilter Criteria1: Syntax, Examples & Tips in ExcelIn VBA, Criteria1 is the first filter condition passed to Excel’s AutoFilter method; for example, Criteria1:="East" keeps rows…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.