The direct answer: in VBA, there is no single “If Not Null” statement because VBA does not have a true NULL data type like a database. Instead, you check for Nothing (for objects), Empty (for uninitialized variants), Null (for variants that explicitly contain Null), or an empty string "". The most common need—checking if a cell is empty—is done with If Not IsEmpty(Range("A1")) Then or If Len(Range("A1").Value) > 0 Then.
Understanding Null, Empty, Nothing, and Empty String
VBA has four distinct “no value” states, and confusing them is a classic source of bugs. Here is what each one means:
- Null: A Variant that explicitly contains Null. This usually comes from a database query or a field that has no value. It is not the same as an empty cell.
- Empty: A Variant that has never been assigned a value. An empty Excel cell returns
Emptywhen read into a Variant. - Nothing: A reference to an object variable that has not been set. Use
If myObject Is Nothing Then. - Empty string (“”): A text value of zero length. A cell containing a formula that returns “” is not empty—it has a string value.
For most Excel tasks, you want to check if a cell is truly empty (no value, no formula). Use IsEmpty or compare to vbNullString (which is the same as “”).
How to Check If a Cell Is Empty in VBA
The most reliable way to test if a cell is empty is the IsEmpty function. It returns True if the cell is empty, and False if it contains any value, including a formula that returns an empty string.
Sub CheckCell()
Dim cell As Range
Set cell = Range("A1")
If IsEmpty(cell.Value) Then
MsgBox "Cell A1 is empty"
Else
MsgBox "Cell A1 has a value"
End If
End Sub
If you need to treat a formula that returns “” as empty, use Len(cell.Value) = 0 instead. This checks the length of the string; an empty string has length 0.
If Len(cell.Value) = 0 Then
' Cell is empty or contains only an empty string
End If
Note: IsEmpty works on a Variant, so IsEmpty(cell.Value) is correct. Do not use cell Is Nothing—that checks if the Range object itself is set, not if the cell has a value.
Checking for Null in VBA (Database Values)
If you are reading data from a database or from a field that can contain Null, use the IsNull function. Null is not the same as Empty. For example, an ADO recordset field may return Null if the database column has no value.
Dim dbValue As Variant
' Assume dbValue is assigned from a recordset
dbValue = recordset.Fields("Column1").Value
If IsNull(dbValue) Then
MsgBox "Database value is Null"
Else
MsgBox "Value: " & dbValue
End If
You can also check for Null using If dbValue Is Null Then—but that is not valid VBA syntax. The correct way is IsNull(dbValue). To check for both Null and Empty, combine conditions: If IsNull(dbValue) Or IsEmpty(dbValue) Then.
Common Mistakes and Misconceptions
One common mistake is writing If Range("A1") <> Nothing Then—this does not work because Nothing is for objects, not values. Another is using If Range("A1").Value = Null Then—this always returns False because comparing a Variant to Null yields Null, which is treated as False in an If statement. Always use IsNull or IsEmpty.
Another misconception: an empty cell is not the same as a cell containing a space character. A cell with a space looks empty but has a string of length 1. Use Trim(cell.Value) = "" to treat spaces as empty.
Practical Examples: Looping Through a Range
You often need to check multiple cells. Here is a loop that counts non-empty cells in column A:
Sub CountNonEmpty()
Dim rng As Range
Dim cell As Range
Dim count As Long
Set rng = Range("A1:A10")
count = 0
For Each cell In rng
If Not IsEmpty(cell.Value) Then
count = count + 1
End If
Next cell
MsgBox "Non-empty cells: " & count
End Sub
If you want to skip cells that contain only whitespace, use If Len(Trim(cell.Value)) > 0 Then.
Using If Not with Null in Worksheet Formulas vs VBA
In Excel worksheet formulas, you use ISBLANK and ISNULL (though ISNULL is not a worksheet function—it is VBA only). In VBA, the equivalent of ISBLANK is IsEmpty. For worksheet cells, there is no Null concept—cells can be empty, contain a value, or contain a formula that returns an error. If you need to check for errors, use IsError.
If you are writing a UDF (User Defined Function) that returns a value to a cell, you can return Null to indicate “no value”, but Excel will display it as 0 or blank depending on context. Better to return Empty or vbNullString.
Handling Null in Conditional Statements
When you have a Variant that might be Null, Empty, or a real value, use a nested check. For example, to safely concatenate a value:
Dim v As Variant
v = Range("A1").Value
If IsNull(v) Then
v = ""
ElseIf IsEmpty(v) Then
v = ""
End If
MsgBox "Value: " & v
Alternatively, use the Nz function if you are in Access VBA, but in Excel you must write your own. A common pattern is If Not IsNull(v) And Not IsEmpty(v) Then to proceed only when there is a real value.
Frequently Asked Questions
Q: How do I check if a cell is not empty in VBA?
A: Use If Not IsEmpty(Range("A1")) Then or If Len(Range("A1").Value) > 0 Then. The first checks for truly empty, the second treats a formula returning “” as empty.
Q: What is the difference between Null and Empty in VBA?
A: Null means a Variant explicitly contains no valid data, often from a database. Empty means a Variant has never been assigned a value. An empty Excel cell returns Empty, not Null.
Q: Can I use If Range("A1") Is Nothing to check for an empty cell?
A: No. Is Nothing checks if an object variable is unset. A Range object always exists if you reference it; you need to check its .Value property.
Q: Why does If Range("A1").Value = Null not work?
A: Because comparing a Variant to Null returns Null, which is not True or False. VBA treats Null in a boolean expression as False, so the condition is never met. Use IsNull(Range("A1").Value).
Q: How do I treat a cell with a space as empty?
A: Use If Len(Trim(Range("A1").Value)) = 0 Then. This trims leading/trailing spaces and checks the length.
Q: What about checking for #N/A errors?
A: Use If IsError(Range("A1").Value) Then and then check CVErr if needed. That is separate from Null/Empty checks.
Related
- Balance the US Budget? How It Works and What It Takes
- Data Entry Jobs Pay: Real Hourly Rates & Salaries
- Generate 100 Random Numbers: How to Do It
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Find and Replace in a Range: Code and TipsTo find and replace text only within a chosen Excel range, use VBA’s Range.Replace method and call it…
- Wait Time in VBA: How to Pause Code CorrectlyIn VBA, the Wait method of the Application object pauses code execution until a specific time of day,…
- Macro Recorder for Windows: Options and How to ChooseA macro recorder for Windows captures repeated actions and replays them, but Windows does not provide one universal…
- Excel VBA InputBox for Dates: How to Validate InputTo ask for a date in Excel VBA, collect the entry as text, validate it, and convert it…
- Excel UserForm: Change Font Size at Design Time or in VBATo change an Excel VBA UserForm’s font size, select the form or a control in the Visual Basic…
- Clear Excel VBA: Contents, Formats, and CellsTo clear cells with Excel VBA, use Range("B2:D20").ClearContents to remove values and formulas while keeping formatting, or Range("B2:D20").Clear…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.