VBA for Not Null: Check and Handle Empty Cells

VBA & macros · Updated November 23, 2026

The direct answer: in VBA, you test for “not null” using IsNull() for database-style nulls, Len() for empty strings, and IsEmpty() for uninitialized variants. For a cell in Excel, the most common check is If Len(Trim(cell.Value)) > 0 Then, which treats both truly empty cells and cells containing only spaces as “not null.”

What “Null” Means in VBA vs. Excel

VBA has several distinct “empty” states, and using the wrong test is a classic source of bugs. In Excel, a cell can be:

  • Empty – the cell has never been edited; its .Value is Empty (a special Variant subtype).
  • Contains an empty string – the cell has a formula like ="" or was cleared with Range.ClearContents after having content.
  • Contains whitespace – e.g., a single space character, which looks empty but is not.
  • Contains a real Null – this only appears when you pull data from a database into a Recordset; Excel cells themselves never hold a true Null.

Because of these differences, you must decide what your code should treat as “empty.” The safest generic check for a cell is Len(Trim(cell.Value)) = 0 for empty, and Len(Trim(cell.Value)) > 0 for not empty.

Using IsNull, IsEmpty, and Len Correctly

Here are the three main functions and when to use each:

  • IsNull(expression) – Returns True only if the expression is the Null value. Use this when reading from a database (e.g., ADO or DAO recordsets). It does not detect an empty cell in Excel.
  • IsEmpty(expression) – Returns True only if the expression is an uninitialized Variant. For a cell, IsEmpty(Range("A1")) returns True only if A1 is truly empty (no value, no formula). It returns False for a cell containing an empty string.
  • Len(string) – Returns the number of characters in a string. Len(Range("A1").Value) returns 0 for both an empty cell and a cell with an empty string. Combining with Trim() removes leading/trailing spaces.

For a Variant variable that might hold a Null from a database, you can combine: If Not IsNull(varValue) Then.

How to Check a Cell for Not Null in Excel VBA

The most common scenario is checking whether a specific cell has content. Here is a robust pattern:

Dim ws As Worksheet
Dim cell As Range
Set ws = ThisWorkbook.Sheets("Sheet1")
Set cell = ws.Range("A1")

If Len(Trim(cell.Value)) > 0 Then
    ' Cell is not empty (contains something other than spaces)
    MsgBox "Cell A1 has content: " & cell.Value
Else
    ' Cell is empty or only spaces
    MsgBox "Cell A1 is empty"
End If

If you want to treat a cell with only spaces as empty (which is usually what you want), always use Trim.

Looping Through a Range and Skipping Empty Cells

A common task is to process only non-empty cells in a column. Use a For Each loop with the Len check:

Dim rng As Range
Dim c As Range
Set rng = ws.Range("A1:A100")

For Each c In rng
    If Len(Trim(c.Value)) > 0 Then
        ' Do something with c
        Debug.Print c.Address & ": " & c.Value
    End If
Next c

This avoids errors when you try to perform operations on empty cells, such as arithmetic or string concatenation.

Common Mistakes and Misconceptions

One frequent error is using IsNull on a cell value. For example, If IsNull(Range("A1")) Then will always return False because a cell value is never Null – even an empty cell returns Empty, not Null. Another mistake is checking cell.Value = "" without Trim, which fails for cells containing spaces. Also, note that Range("A1").Text returns the formatted text, which can be an empty string even if the underlying value is a number; use .Value for logic.

Another misconception: IsEmpty works on cells, but it only detects the Empty state. If a cell contains a formula that returns "", IsEmpty returns False, which may surprise you. Always test with Len if you want to treat formula results as empty.

Handling Nulls from Database Recordsets

When you query a database using ADO or DAO, fields can contain Null (the database equivalent of missing data). In VBA, you must check for Null before assigning the value to a variable or using it in a string. Example:

Dim rs As ADODB.Recordset
Dim fldValue As Variant
' ... assume rs is opened ...

If Not IsNull(rs.Fields("CustomerName").Value) Then
    fldValue = rs.Fields("CustomerName").Value
Else
    fldValue = "No name"
End If

You can also use Nz() in Access VBA, but in Excel VBA that function is not available. Instead, use IIf(IsNull(rs.Fields(0)), "", rs.Fields(0)).

Comparing VBA Null Checks with Worksheet Functions

Excel’s worksheet functions offer similar tests, but they work differently. For example, ISBLANK(A1) returns TRUE only if A1 is truly empty (no value, no formula). It returns FALSE for a cell containing ="". In VBA, you can use Application.WorksheetFunction to call these, but it’s usually simpler to use VBA’s native functions. If you need to check a range of cells for any blanks, you might use COUNTBLANK, but for per-cell logic, stick with Len.

Frequently Asked Questions

Q: Why does my VBA code treat a cell with a formula that returns “” as not empty?
A: Because the cell’s .Value is an empty string, not Empty. IsEmpty returns False for an empty string. Use Len(cell.Value) = 0 to catch both empty cells and empty strings.

Q: How do I check if a cell is not null and not an empty string in one line?
A: Use If Len(Trim(cell.Value)) > 0 Then. This covers empty cells, empty strings, and whitespace-only cells. For a database Null, use If Not IsNull(var) Then.

Q: What is the difference between IsNull and IsEmpty in VBA?
A: IsNull checks for the Null value, which only comes from databases. IsEmpty checks for an uninitialized Variant, which is the default state of a cell that has never been edited. They are not interchangeable.

Q: Can I use If cell.Value <> Nothing Then to check for not null?
A: No. Nothing is for object references, not values. Use IsNull or Len as described.

Q: How do I handle a cell that contains a zero-length string from a CSV import?
A: CSV imports often put empty fields as empty strings. Use Len(cell.Value) = 0 to detect them. If you want to treat them as truly empty, you can clear them with If Len(cell.Value) = 0 Then cell.ClearContents.

Q: What is the fastest way to check a large range for non-empty cells?
A: Use SpecialCells(xlCellTypeConstants) and SpecialCells(xlCellTypeFormulas) to get a range of all non-empty cells, then loop through that. For example: Set nonEmpty = ws.Cells.SpecialCells(xlCellTypeConstants). This is much faster than checking each cell individually.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.