Excel VBA If Null: Check Empty Cells and Variables
In this article
VBA does not have a null keyword in the way some languages do. What people mean by "if null" is usually one of three checks: whether a cell is empty, whether a Variant variable has never been given a value, or whether a database field came back as Null. Each needs a different test, and using the wrong one is the most common reason an If statement silently does the opposite of what you expect. This guide shows the correct test for each case, from the simplest cell check to the database-style Null.
What You'll Need
- Excel with the Developer tab visible; if it is not there, enable it under File > Options > Customize Ribbon by ticking Developer in the right-hand list. Labels can differ slightly by version and region.
- A workbook saved as .xlsm or .xlsb, because macros cannot be stored in a standard .xlsx file.
- Basic familiarity with opening the VBA editor (Alt+F11) and inserting a module (Insert > Module).
Step-by-Step
Decide what kind of "null" you are testing. An empty worksheet cell is not Null in the VBA sense; it reads as an empty Variant or as an empty string depending on how you read it. A Variant variable that was declared but never assigned is Empty. A field returned from a database or an ADO recordset can be true Null. Pick the matching test before writing code.
Test an empty cell with IsEmpty. Read the cell into a Variant first, because IsEmpty only works on Variants:
Dim v As Variant v = Range("A1").Value If IsEmpty(v) Then MsgBox "A1 is empty" End IfThis is the cleanest test for a cell that has never had anything typed into it or has had its contents deleted.
Test a cell that may contain an empty string. A formula such as =IF(B1="","",B1) returns a zero-length string, not Empty. IsEmpty returns False for it because the cell holds a formula result. Use Len instead:
If Len(Range("A1").Value) = 0 Then MsgBox "Nothing visible in A1" End IfLen catches both Empty and empty-string cases, so it is the safer general-purpose check for user-facing data.
Use IsNull for database Nulls. A true Null appears when you read data through ADO, DAO or QueryTable. IsNull is the only test that detects it:
If IsNull(rs.Fields("Notes").Value) Then MsgBox "No note stored" End IfIsNull returns False for Empty and for empty strings, so it will not help with worksheet cells.
Never compare to Null with an equals sign. An expression like
If x = Null Thenis always False, even when x is Null, because any comparison with Null yields Null, which VBA treats as False. This single mistake is why many "if null" routines never fire. Use IsNull, IsEmpty or Len as shown above instead.Branch cleanly with ElseIf when several cases are possible. A field can be Null, empty string or a real value, and you may want to handle each differently:
If IsNull(x) Or IsEmpty(x) Then result = "missing" ElseIf Len(Trim(CStr(x))) = 0 Then result = "blank" Else result = CStr(x) End IfTrim removes spaces so that a cell containing only spaces is also treated as blank.
Write a small helper function for repeated checks. If you test the same condition in many procedures, put it in one place so the rules stay consistent:
Function IsBlank(v As Variant) As Boolean If IsNull(v) Then IsBlank = True ElseIf IsEmpty(v) Then IsBlank = True ElseIf Len(Trim(CStr(v))) = 0 Then IsBlank = True Else IsBlank = False End If End FunctionCall it with
If IsBlank(Range("A1").Value) Then.Guard against errors on non-text values. CStr fails on an Error value such as #N/A. If your range can contain errors, check with IsError before converting, or read the cell's Text property instead of Value. Reading Text returns what is displayed and never raises a type mismatch.
Run and step through. Put the cursor inside your procedure and press F8 to run one line at a time. Watch the Locals window (View > Locals) to confirm the variable really holds Empty, Null or a string at the moment your If statement executes. This removes guesswork about which branch should run.
Common Mistakes
Using
= Nullor= ""as the null test. The first never matches; the second misses true Nulls and Cells holding only spaces.Calling IsEmpty on a String variable. IsEmpty only works on Variants, so declare the variable as Variant if you intend to test it that way.
Assuming a blank-looking cell is Empty. A cell with a formula returning "" is not Empty, and neither is a cell containing a single space.
Testing UsedRange or the whole column and expecting empty-looking areas to pass an IsEmpty check; they may hold formatting or stale values. Test the specific cells you care about.
Forgetting that deleting a cell's contents leaves the cell not Empty in the IsEmpty sense only after a genuine clear; a Clear method removes values, but a cell that previously held a formula can still behave differently from one that was never touched. When in doubt, use Len.
Tips
Use
Len(Trim(CStr(v))) = 0as your default test for worksheet data. It covers Empty, Null, empty strings and whitespace in one line, which is what most people actually want when they say "if null".Turn on Option Explicit at the top of every module (Tools > Options > Require Variable Declaration). It forces you to declare variables, which prevents the misspelled-variable bug that makes a null test always pass.
Store your test in a helper function rather than repeating it. When the rule changes, you edit one place.
If you only need to skip blank rows in a loop, test the whole row with
If Application.CountA(Rows(i)) = 0 Thenand jump to the next iteration.Comment the reason for each null check. Six months later, "why is this here" is harder to answer than "what does this do".
Troubleshooting
Your If never fires. The most likely cause is a comparison to Null or a test on the wrong data type. Replace it with IsNull, IsEmpty or Len and step through with F8 to see the actual value.
You get a type mismatch error. CStr is being applied to an Error value or an object. Check with IsError first, or read the cell's Text property.
The macro treats a blank-looking cell as filled. It probably contains a formula returning an empty string, a space, or a non-printing character. Test with Len(Trim(...)) and inspect the cell with CLEAN or TRIM in a worksheet formula to confirm.
Database fields still crash your code. Reading a Null into a String variable raises an error because Null cannot be assigned to a String. Assign to a Variant first, test with IsNull, then convert.
IsEmpty returns False for a Variant you never set. It was probably assigned somewhere earlier in the procedure. Use the Locals window to trace it.
Frequently Asked Questions
Is there a Null keyword in VBA?
No. VBA has the Null value (used mainly for database fields), the Empty value for unassigned Variants, and empty strings for text. There is no literal you can type as a keyword, so you test for these with IsNull, IsEmpty and Len.
What is the difference between IsEmpty and IsNull?
IsEmpty is True for a Variant that has never been assigned a value, such as a blank cell read into a Variant. IsNull is True only for a true Null, typically from a database. They never both return True for the same value.
How do I check if a cell is empty in VBA?
Read it into a Variant and use IsEmpty, or use Len(Range("A1").Value) = 0, which also catches formulas returning empty strings. Len is the more reliable choice for real-world sheets.
Why does If x = Null Then never work?
Any comparison involving Null evaluates to Null, and VBA treats Null as False in an If statement, so the branch is skipped even when x is Null. Use IsNull(x) instead.
How do I check whether a whole row is blank?
Use Application.CountA on the row and compare to zero. This counts non-empty cells, so a result of zero means the row holds nothing.
Can I use IsEmpty on a String variable?
No. IsEmpty only accepts Variants. For a String, test with Len(s) = 0, and declare the variable as Variant if you also need to detect a never-assigned state.