Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: How to Check If a Value Is Not Null

· 7 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To test for "not null" in Excel VBA, check the value with VBA's built-in Null function rather than comparing it to the word Null. A direct comparison such as If myValue <> Null Then always evaluates to Null, not True or False, so the branch is skipped even when the value is perfectly valid. The reliable pattern is If Not IsNull(myValue) Then, or, if the value comes from a cell, combine it with a check on IsEmpty and an empty-string test so you also catch blank cells. The steps below walk through each case, because "null" means different things depending on where the value comes from: a Variant holding a database Null, an empty cell, or a zero-length string.

What You'll Need

  • A desktop copy of Excel with the VBA editor available. VBA is not available in Excel for the web, and it is limited or unavailable in some mobile versions, so use Windows or macOS Excel.
  • Macro-enabled access: a standard .xlsm workbook, or a macro-enabled template. Save a copy first if you are testing on real data.
  • The Developer tab enabled, or you can press Alt+F11 (Windows) to jump straight to the editor. On macOS the shortcut is different, so use Tools > Macro > Visual Basic Editor from the menu bar if the shortcut does not work.
  • Basic familiarity with an If ... Then ... End If block and with adding a module.

Step-by-Step

  1. Decide what "null" means for your variable. In VBA, a cell that has never been used is empty. A cell containing a formula that returns an empty string is not empty, but it looks blank. A Variant read from certain sources (a database query, a recordset field, some external data) can hold a genuine Null. Mixing these up is the number one reason a check silently fails.
  2. Open the VBA editor and add a module. Press Alt+F11 on Windows, then choose Insert > Module. Menu labels can differ slightly by version, device and region, so look for the Insert menu if the wording looks unfamiliar. Everything you write in the next steps goes in this module.
  3. Write the basic not-null test for a Variant. Declare the variable as Variant and use the built-in function: Dim v As Variant, then If Not IsNull(v) Then ... End If. IsNull returns True only when the value is a genuine Null, so Not IsNull(v) is True for everything else, including zero, an empty string and False. Note that a plain If v Then also fails on Null, so do not shortcut it.
  4. Test a cell for blank or empty. Read the cell into a Variant first: Dim c As Variant then c = Range("A1").Value. Use If Not IsEmpty(c) And Len(c) > 0 Then to confirm the cell actually holds something visible. IsEmpty on its own is not enough, because a cell holding a formula that returns "" is not Empty yet displays nothing.
  5. Combine both checks when the source is unknown. If a value might be a database Null or a blank cell, test Not IsNull(c) And Not IsEmpty(c) And Len(c > 0) style conditions in one line. VBA does not short-circuit And, so evaluate carefully: passing a Null to Len returns Null and can make the whole condition Null. The safe form is to check IsNull first with a separate If, or wrap each test so Null is never handed to a string function.
  6. Handle the zero-length string explicitly. After confirming a value is not Empty or Null, compare against "" using Len(c) > 0. This catches cells and variables that contain an empty string, which many users perceive as blank even though VBA does not.
  7. Use IsError if the value may be an error. A cell containing #N/A or #VALUE! is neither Null nor Empty. Check with If Not IsError(c) Then before doing anything else, or those errors propagate into your comparisons.
  8. Test the logic before shipping it. Run a small test procedure that prints results to the Immediate window: set a variable to Null, to "", to 0, to a text value, and check each with your condition. The Immediate window opens with Ctrl+G on Windows (View > Immediate Window). If a branch behaves unexpectedly, the condition is the cause, not your data.
  9. Add error handling around anything external. If the value comes from a query, an external workbook or an API call, wrap the read in On Error GoTo and a labelled handler. Connection failures produce errors, not Null, so a pure null check will not protect you.
  10. Save as a macro-enabled workbook. Use File > Save As and choose the macro-enabled format, so your code survives closing the file.

Common Mistakes

  • Comparing with = Null or <> Null. Both always return Null, never True. Any expression containing Null is Null, and Null is not equal to anything, not even itself. Always use IsNull.
  • Confusing Empty with Null. An unused cell is Empty. A Variant from a database can be Null. They are different states, and one test does not cover the other.
  • Assuming IsEmpty catches formula blanks. A formula returning "" is a zero-length string, not Empty, so code that only checks IsEmpty treats it as a real value.
  • Relying on short-circuit evaluation. VBA evaluates every part of an And condition, so a Null in the second test can still poison the result.
  • Passing Null to string functions. Len, Trim, InStr and similar functions return Null when given Null, which then travels silently through your logic.
  • Dimming as String when a value can be Null. Assigning Null to a declared String raises an error. Use Variant for anything that may be null.

Tips

  • Write one small helper function such as Function HasValue(v As Variant) As Boolean that returns False for Null, Empty, error values and zero-length strings. Call it everywhere and keep the messy logic in one place.
  • Use Nz-style replacement only if your project references the Access library; in plain Excel, the cleaner approach is If IsNull(v) Then v = "" before using the value.
  • Use IsNumeric after a not-null check when the value must be a number, since an empty string passes a not-null test but is not numeric.
  • When looping over a range, read values into a Variant array in one go rather than cell by cell; it is faster and the array keeps the Empty/Null distinction intact.
  • Keep a comment on each condition saying which case it guards, so a later edit does not quietly remove an important test.

Troubleshooting

The condition never runs. Check whether you used = Null or <> Null. Replace with IsNull and negate it.

The condition always runs, even on blank cells. A Formula returning "" passes a Not IsNull test. Add Len(c) > 0 and confirm you are not testing an error value.

"Invalid use of Null" error. Null reached a String variable or a string function. Dim it as Variant and test before converting.

Results wrong only for cells with errors. Add an IsError check first. Errors are separate from blank and null.

Code works on some rows and not others. Print the tested value and its VarType to the Immediate window. That tells you whether the cell is Empty, text, a number or an error, and the fix follows from there.

If that did not work, step through the procedure line by line with F8 on Windows (Debug > Step Into) and watch the condition evaluate, rather than guessing.

Frequently Asked Questions

Why does If x <> Null Then never fire?

Because any comparison with Null yields Null, not True. Use If Not IsNull(x) Then.

Is IsEmpty the same as IsNull?

No. IsEmpty is True only for an uninitialized Variant, such as an unused cell. IsNull detects an actual Null value, typically from a database.

How do I treat a blank-looking formula cell as empty?

Test Len(c) = 0 after confirming the value is not Null or an error.

Can I use IsNull on a String variable?

A String cannot hold Null, so the test is pointless there. Declare the variable as Variant when Null is possible.

Does VBA short-circuit And?

No. All parts of the expression are evaluated, so arrange separate If blocks to avoid passing Null into a function.

What about ranges that contain multiple cells?

Read Range.Value into a Variant; if it holds more than one cell, it returns an array. Test each element in a loop.