In Excel VBA, a Range is Nothing when an object variable does not currently refer to a range object. Check that state with If rng Is Nothing Then; assign a range with Set rng = Worksheets("Data").Range("A1"). This is about the variable’s object reference, not whether the cell is blank.
What Nothing means in VBA
Range is an object type: it represents a cell or group of cells and exposes properties such as Value, Address, and Interior. A variable declared Dim rng As Range can hold a reference to such an object. Until VBA assigns one, the variable has no object reference, and its value is Nothing.
VBA uses Nothing as a special keyword for an object reference that points to no object. It is not a range, a cell value, or a string. For example, Set rng = Nothing removes the reference held by rng; it does not delete, clear, or change any cells. Another variable that refers to the same cells is unaffected.
The distinction matters because object variables are assigned with Set. Use Set rng = ws.Range("A1") to make rng refer to a range. Trying to use a normal value assignment for an object reference is not the right syntax. When a local object variable goes out of scope at the end of its procedure, VBA releases that variable’s reference; explicitly setting it to Nothing is usually optional, though it can make a reset or cleanup step clear.
A range reference is not the same as cell content
A range object can exist even when its cell contains nothing. If rng refers to A1 and A1 is blank, then rng Is Nothing is still False. The reference is valid; the cell’s value is empty. Conversely, an unassigned rng is Nothing regardless of what data is elsewhere in the worksheet.
Keep these separate concepts in mind:
Nothingmeans an object variable has no object reference.Emptyis a Variant state that commonly indicates a Variant has not been assigned a value.IsEmpty(value)tests whether a Variant is Empty; it does not test whether a Range variable is Nothing.- A blank cell, a formula returning an empty string, zero, and a cell containing spaces are different cell-content cases. Inspect the relevant range property, such as
ValueorValue2, to handle those cases.
For instance, checking whether a cell is blank is a question about its contents, not whether the range exists. First ensure the range reference is valid, then decide how your code should treat the cell’s value. A formula returning "" may look blank while still containing a formula, so checking only the displayed appearance may not answer the question you mean to ask.
How to test whether a Range is Nothing
Use VBA’s Is operator to compare an object reference with Nothing. The standard test is If rng Is Nothing Then. The inverse is If Not (rng Is Nothing) Then. These tests ask whether the variable refers to an object; they do not compare cell values.
Do not write If rng = Nothing Then. The equals sign is used for value comparisons, whereas Is tests object identity and Nothing references. Using = with an object can cause an error or lead VBA to evaluate a default property instead of checking the reference. A clear guard clause prevents later code from trying to use an unassigned object.
Save a copy of the workbook before running a macro. Macros cannot be undone with Ctrl+Z. This small example checks an uninitialized variable before any range property is accessed:
Sub CheckRangeReference()
Dim rng As Range
' A declared object variable has no reference until Set assigns one.
If rng Is Nothing Then
MsgBox "No range has been assigned."
Exit Sub
End If
MsgBox rng.Address
End Sub
The message branch runs because this procedure never assigns an object to rng. If you add Set rng = ThisWorkbook.Worksheets("Data").Range("A1") before the test, and the worksheet exists, the test is False and the address can be displayed. Qualifying the worksheet avoids relying on whichever sheet happens to be active.
Assigning, resetting, and qualifying a Range
To create a useful reference, assign a range returned by a worksheet or workbook object. For example, Set rng = ws.Range("B2:B10") makes rng refer to those cells. The range reference is separate from the contents: assigning it does not copy values or alter the worksheet.
When practical, qualify the range with a worksheet variable. An unqualified expression such as Range("A1") refers to the active sheet, which can change when a user clicks another sheet or when code activates one. Similarly, ActiveCell depends on the current selection. Explicit references make procedures more predictable, especially when a workbook has multiple sheets.
Use Set rng = Nothing when you intentionally want to clear that variable’s reference, such as before reusing it in a sequence of object searches. It does not clear the cells. If you later access rng.Value without assigning another range, VBA raises an error because the reference is missing. A Nothing check should therefore come before property or method calls whenever the reference may be absent.
Assigning a range expression can itself fail for other reasons. A misspelled worksheet name, a closed workbook, or an invalid address can cause a runtime error before the variable receives a usable range. Is Nothing is not a universal error handler: it checks a reference after code has run successfully, but it does not automatically catch failed worksheet lookups or invalid object expressions.
Why Find commonly returns Nothing
A frequent real-world use of this check is Range.Find. The method returns a Range object for a match and returns Nothing when it cannot find the requested content. Code should test the result before reading its address, value, or other properties. Otherwise, a search with no match can lead to an attempt to use a nonexistent object.
Save a copy of the workbook before running this macro; macros cannot be undone with Ctrl+Z. It assumes the workbook contains a worksheet named Data. The search specifies its main options rather than relying on search settings left from an earlier Find operation:
Sub FindQuarterlyLabel()
Dim ws As Worksheet
Dim hit As Range
' The Data worksheet must exist in this workbook.
Set ws = ThisWorkbook.Worksheets("Data")
Set hit = ws.Cells.Find(What:="Quarterly", _
After:=ws.Cells(1, 1), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
' Find returns Nothing when there is no matching value.
If hit Is Nothing Then
MsgBox "Quarterly was not found."
Else
MsgBox "Found at " & hit.Address
End If
End Sub
Here, LookAt:=xlWhole asks for a whole-cell match, so a cell containing “Quarterly results” is not an exact match. LookIn:=xlValues searches displayed values rather than formulas as formula text. Change those options to match the task. If the search should cover only part of the worksheet, call Find on that specific range rather than on all of ws.Cells.
Common causes and the Object variable error
The message “Object variable or With block variable not set” is run-time error 91. It often means code tried to use a property or method through an object variable that is Nothing. For a Range, the risky operation might be rng.Value, rng.Select, or hit.Address after a search returned no match.
Common causes include declaring a variable but forgetting its Set assignment, assigning it only inside a branch that did not run, clearing it with Set rng = Nothing and then using it, or failing to check a method such as Find that can return no object. A related issue is using a local variable after its intended setup code was skipped due to an earlier conditional path.
When debugging, pause at the error and inspect the exact variable named in the failing expression. The Locals window and the Watch window in the Visual Basic Editor can help show whether it currently has a reference. Then trace every path that should assign it. A check immediately before use can prevent error 91, but the lasting fix is to ensure the program’s logic handles the case where the object was never obtained.
Do not treat every run-time error as proof that the range is Nothing. A valid range can still cause other errors, and a failed expression that was meant to assign a range may stop execution before a later check. Check the failing line and the object’s assignment path rather than adding a broad error-suppression statement that hides unrelated problems.
Choosing the right check for the job
Use the test that matches the question you are asking. If the question is “Did this object-producing operation return a range?”, test Is Nothing. If the question is “Is the cell empty?”, inspect the cell’s value or formula according to the required definition of empty. If the question is “Did Find locate a match?”, test the returned Range before using it.
It is also useful to distinguish a missing reference from an invalid reference. A variable can be Nothing because no object was assigned. A range that was successfully assigned to a worksheet cell is a real reference, even if the cell is blank. If the workbook or worksheet context is wrong, the assignment itself may fail; a Nothing test after that line is not a substitute for handling the assignment error.
For reusable procedures, pass a Range as an argument and validate it at the point where it is used if callers might supply no object. For routines that require a range, document that requirement and fail with a helpful message rather than allowing a confusing error later. Do not silently select another cell as a fallback unless that behavior is genuinely appropriate for the task.
Frequently Asked Questions
Can a worksheet cell itself be Nothing?
No. A cell is represented in VBA by a Range object. The Range variable that refers to the cell can be Nothing, but a blank cell is still represented by a valid Range object.
Does Set rng = Nothing delete or clear a cell?
No. It removes the object reference held by the variable. To clear cell contents, use an appropriate Range method such as ClearContents on a valid range; do not use Nothing for that purpose.
Why does Find return Nothing even when the text seems to be there?
The search settings matter. A whole-cell match differs from a partial match, and searching values differs from searching formulas. Check the searched range, spelling, and options such as LookAt and LookIn, then test the returned object before using it.
Should I set every Range variable to Nothing at the end?
Usually not for a local variable that goes out of scope when its procedure ends. Explicitly setting it to Nothing can still be useful when you want to release or reset a reference before the procedure finishes. It never clears the worksheet cells.
Can I use IsEmpty to test a Range variable?
No. IsEmpty is intended to test a Variant value. Use rng Is Nothing for an object reference, and use a suitable value or formula check for the contents of a cell.
Related
- Excel Goal Seek Shortcut: Open It, Set a Target, Solve
- How to Enable Solver in Excel on Windows and Mac
- Flash Fill in Excel: Keyboard Shortcut and How to Use It
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Array Cheat Sheet: Syntax, Bounds, and ExamplesA VBA array stores multiple values under one variable name, and the essential pattern is to declare it,…
- Outlook VBA: Pull Email Data into ExcelTo pull email data from Outlook into Excel, run a VBA macro in Excel that connects to classic…
- Excel Macro to Move Down One Cell in the Active SheetTo move the active cell down one row in Excel, run a VBA macro that selects ActiveCell.Offset(1, 0).…
- Excel VBA Progress Bar: How to Build a Responsive OneAn Excel VBA progress bar shows how much of a long-running macro has finished. For a visual bar,…
- Vba loop through rows?Learn how to loop through Excel rows with VBA, find the last row, handle conditions and deletions, and…
- VBA Range.Clear: What It Clears, Keeps, and How to Use ItIn Excel VBA, Range.Clear clears the specified cells, removing their values or formulas and their cell formatting, along…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.