Excel VBA Not IsNull: How to Check for Non-Null Values
In this article
To perform a "Not IsNull" check in Excel VBA, you wrap the IsNull function inside the Not operator, like this: If Not IsNull(variable) Then. This logic allows your code to execute only when a variable or field contains an actual value, preventing the common "Invalid use of Null" or "Type mismatch" errors that occur when VBA attempts to process a Null value as a string or number.
What You'll Need
- Microsoft Excel installed on a Windows PC.
- The Developer tab enabled in the Excel Ribbon.
- Basic familiarity with the VBA Editor (VBE).
- A dataset containing either empty cells or data retrieved from an external database (where Nulls are most common).
Step-by-Step
- Open the VBA Editor: Press
Alt + F11on your keyboard to launch the Visual Basic for Applications editor. - Insert a New Module: In the project explorer on the left, right-click on your workbook name, select
Insert, and then clickModule. This provides a clean space to write your custom procedure. - Declare Your Variables: To test IsNull, you must use a
Variantdata type. This is because specific types likeInteger,String, orDoublecannot hold a Null value; they will either default to 0 or an empty string, which makes IsNull return False. UseDim myValue As Variant. - Assign a Value: For testing purposes, assign a value to your variable. To simulate a Null (which is common when pulling data from Access or SQL databases), you can use a database recordset or simply leave a variable uninitialized in certain contexts. For a standard Excel cell check, remember that a blank cell is technically
Empty, notNull, though IsNull handles certain object-based returns differently. - Implement the Not IsNull Logic: Write the conditional statement. The
IsNullfunction returnsTrueif the expression is Null. By addingNotbefore it, you invert the result.
If Not IsNull(myValue) Then' Place the code you want to run if the value exists hereEnd If - Add an Else Clause for Safety: To ensure your program doesn't simply skip a step without notification, add an
Elseblock to handle the Null cases. This is critical for debugging data imports.
If Not IsNull(myValue) ThenMsgBox "Value found: " & myValueElseMsgBox "The value is Null!"End If - Run the Macro: Press
F5or click the green "Run" arrow in the toolbar to execute your code and verify that the logic correctly identifies the presence of data.
Common Mistakes
The most frequent error developers make is confusing Null with Empty or an Empty String (""). In VBA, these are three distinct states. A Null value indicates that data is missing or unknown (typically from a database). An Empty value means the variable has been declared but not yet initialized. An empty string is a string that has been initialized but contains no characters.
If you use If Not IsNull(Range("A1").Value) Then on a blank Excel cell, it will likely return True. This is because Excel cells generally return an Empty value or a zero-length string, not a Null. To check if an Excel cell is not blank, you should use If Range("A1").Value <> "" Then or If Not IsEmpty(Range("A1")) Then. Using IsNull on standard worksheet cells is a common logic error that leads to code running when it should have stopped.
Another mistake is attempting to use IsNull with strictly typed variables. If you declare Dim x As String, the variable x can never be Null; it will be "" by default. Attempting to check IsNull(x) will always return False, rendering your Not IsNull check useless. Always use Variant when you expect potential Nulls from external data sources.
Tips
When dealing with large datasets, combine Not IsNull with other checks to create a "robust validation" line. For example, if you want to ensure a value is not Null, not Empty, and not a blank string, you can chain them: If Not IsNull(var) And Not IsEmpty(var) And var <> "" Then. This ensures that no matter how the "emptiness" is defined in the source data, your code only processes actual content.
For cleaner code, consider creating a custom helper function. If you find yourself writing Not IsNull(...) dozens of times, create a function called HasValue(val As Variant) As Boolean that returns True only if the value passes all your "not empty" criteria. This makes your main logic much easier to read and maintain.
If you are working with Database Recordsets (DAO or ADO), Not IsNull is the gold standard. When looping through rows, always check for Nulls before attempting to perform calculations or string concatenations. A single Null value encountered during a mathematical operation (like 10 + Null) will result in a Null output, which can cascade through your entire report and leave you with blank results without triggering an explicit error.
Troubleshooting
If your Not IsNull check is returning True even when the cell looks empty, the cell likely contains a zero-length string or a space. To fix this, use the Trim function: If Not IsNull(myValue) And Trim(myValue) <> "" Then. This removes leading and trailing spaces, ensuring that cells containing only whitespace are treated as empty.
If you receive a "Type Mismatch" error despite using Not IsNull, check the line of code immediately following the check. You might be passing the variable into a function that requires a specific type (like CInt or CDbl). If the variable is Empty (but not Null), the Not IsNull check passes, but the conversion function fails. In this case, add And Not IsEmpty(myValue) to your conditional statement.
If your code is skipping the Then block entirely when you believe there is data, check if the data is being read as a Null object from a database. Use Debug.Print TypeName(myValue) in the Immediate Window (Ctrl + G) to see exactly what VBA thinks the variable is. If it says Null, your Not IsNull logic is working correctly, and the issue lies with the data source.
Frequently Asked Questions
What is the difference between IsNull and IsEmpty?
IsNull checks if a variable is assigned the Null keyword, which represents missing or unknown data (common in databases). IsEmpty checks if a Variant variable has been initialized. A declared but unused Variant is Empty, but not Null.
Can I use Not IsNull with a String variable?
No. A variable declared as String cannot hold a Null value. It will either be an empty string ("") or contain text. To use IsNull, the variable must be declared as a Variant.
Does Not IsNull work for checking blank cells in a sheet?
Generally, no. Excel worksheet cells return Empty or a zero-length string rather than Null. For cells, use If Range("A1").Value <> "" Then or If Not IsEmpty(Range("A1")) Then.
Why does my code crash even after using Not IsNull?
This usually happens because the value is not Null, but it is a type that the subsequent code cannot handle (e.g., a string in a math formula). Use IsNumeric() or IsDate() in addition to Not IsNull to ensure the data type is correct for the operation.
Is there a shorter way to write Not IsNull?
In VBA, Not IsNull() is the standard and most readable way to perform this check. While you can create custom functions to shorten it, there is no built-in "IsNotNull" function in the native VBA library.