The short answer: If Not Intersect(Target, Range("A1:A10")) Is Nothing Then is the standard VBA pattern to run code only when a changed cell falls inside a specific range. It works because Intersect returns a Range object if the two ranges share any cells, and Nothing if they don’t; the Not ... Is Nothing check turns that into a true/false condition.
What Does Intersect Actually Do?
Intersect is a built-in Excel VBA function that takes two or more range arguments and returns a new range representing the overlapping cells. If there is no overlap, it returns the special value Nothing – not an empty range, not an error, but literally an object variable that points to nothing.
Because Nothing is an object state, you cannot compare it with = or <>. You must use the Is operator, which checks whether two object variables refer to the same object (or both refer to nothing). That is why the pattern is always If Not Intersect(...) Is Nothing Then – it reads as “if the intersection is not nothing.”
Here is a minimal example that triggers a message box when you edit any cell in B2:B20:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim watchRange As Range
Set watchRange = Me.Range("B2:B20")
If Not Intersect(Target, watchRange) Is Nothing Then
MsgBox "You changed a cell in B2:B20"
End If
End Sub
Remember: this code goes in the worksheet’s code module (right-click the sheet tab → View Code), not in a standard module. Also, always save a copy of your workbook before running a macro – macros cannot be undone with Ctrl+Z.
Why Use “Not Intersect Is Nothing” Instead of Other Methods?
Beginners often try to check if a target cell’s address matches a range, like If Target.Address = "$A$1". That fails when the user selects multiple cells or when you need to allow any cell within a block. The Intersect method is robust because it handles multi-cell selections, merged cells, and entire rows/columns gracefully.
Another alternative is to loop through each cell in Target and test membership, but that is slower and more verbose. The Intersect one-liner is both faster and clearer.
Common Scenarios Where You’ll Use It
- Worksheet_Change event: react only when a specific input range is edited (e.g., data entry cells).
- Worksheet_BeforeDoubleClick: allow double-click only on certain columns.
- Worksheet_SelectionChange: highlight the row or column of the active cell only when it’s inside a table.
- Validation: check if the changed cell is inside a named range, like
Set watchRange = Me.Range("InputArea").
For example, to prevent accidental edits to a protected formula range, you might use If Not Intersect(Target, Me.Range("C1:C5")) Is Nothing Then to show a warning and then revert the change.
How to Handle Multiple Ranges and Edge Cases
You can pass more than two ranges to Intersect – it will return the overlap of all of them. For instance, Intersect(Target, Range("A1:C10"), Range("B1:B5")) returns only cells that are in both the A1:C10 block and B1:B5 (so B1:B5).
A common edge case: if Target is a multi-cell selection (e.g., the user pastes a block of cells), Intersect returns the overlapping subset, and your code runs once for the whole block, not per cell. That is usually what you want. If you need to process each cell individually, loop through the intersection:
Dim inter As Range
Set inter = Intersect(Target, Me.Range("A1:A10"))
If Not inter Is Nothing Then
Dim cell As Range
For Each cell In inter
cell.Value = UCase(cell.Value)
Next cell
End If
Another edge case: if the user deletes a row or column, Target can be an entire row or column. Intersect still works fine; it just returns the overlapping cells.
How to Choose Between Intersect and Other Event Patterns
When you need to react to changes anywhere in a worksheet, you don’t need Intersect at all – just put code directly in the Worksheet_Change event. But when you want to limit the reaction to specific cells, Intersect is the cleanest filter.
If you need to react to changes in multiple disjoint ranges, you can either call Intersect multiple times or use a Select Case on the intersection’s address. For example:
If Not Intersect(Target, Me.Range("A1:A5")) Is Nothing Then
' code for A1:A5
ElseIf Not Intersect(Target, Me.Range("C1:C5")) Is Nothing Then
' code for C1:C5
End If
But note: ElseIf stops at the first true condition, so if a selection overlaps both ranges, only the first block runs. If you need both, use two separate If statements.
Common Mistakes and How to Avoid Them
- Forgetting the
Isoperator: writingIf Not Intersect(...) = Nothingwill raise a type mismatch error because you’re comparing an object to a value. - Using
Targetwithout qualifying it: inside a worksheet event,Targetis aRangeobject, but if you copy the code to a standard module, it won’t work. Always keep event procedures in the sheet module. - Not handling multi-cell changes: if you use
Target.Addressto check a single cell, a paste operation will break your code. UseIntersectinstead. - Forgetting to exit the event: if your code modifies cells inside the watched range, it will trigger the event again, causing an infinite loop. Use
Application.EnableEvents = Falseat the start and set it back toTrueat the end.
Example of safe event handling:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("A1:A10")) Is Nothing Then
Application.EnableEvents = False
' your code here
Application.EnableEvents = True
End If
End Sub
Frequently Asked Questions
Can I use Intersect with named ranges?
Yes. Replace the range reference with the named range’s name, e.g., Set watchRange = Me.Range("MyNamedRange"). The named range can be workbook-level or worksheet-level; just make sure you reference it correctly (e.g., ThisWorkbook.Names("MyNamedRange") if it’s workbook-level, but Me.Range("MyNamedRange") usually works too).
What if I want to run code when a cell is NOT in a range?
Use the same pattern but invert the logic: If Intersect(Target, Range("A1:A10")) Is Nothing Then – this runs when the target does not overlap the range. For example, to warn when a user edits outside a data entry area.
Does Intersect work with entire rows or columns?
Yes. If you pass Range("A:A") as one argument, it will intersect with any target that falls in column A. This is useful for reacting to changes in any cell of a column, regardless of row.
Why does my code run twice when I change a cell?
This usually happens because you are modifying a cell inside the watched range, which triggers the event again. Use Application.EnableEvents = False as shown above to prevent re-entry. Another cause is having the same event code in both the worksheet module and the workbook module – check both locations.
Can I use Intersect in a standard module?
Yes, you can call Intersect from any module, but it requires valid Range objects. In a standard module, you’d typically use ThisWorkbook.Sheets("Sheet1").Range(...) instead of Me.Range because Me doesn’t exist there.
Is there a performance difference between Intersect and looping through cells?
Yes. Intersect is a single function call that returns a range object, while looping through each cell in Target can be slow if the selection is large. For most cases, Intersect is significantly faster and more readable.
Related
- How to Make Random Numbers in Python
- Sitting Balance Test Calculator: How to Use It
- Home Expense Tracking Template in Excel: How to Build One
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Convert String to Uppercase with UCase ExamplesUse VBA’s UCase function to convert a string to uppercase; for a result explicitly typed as a String,…
- How to Write a Macro in Excel (Step-by-Step)Writing a macro in Excel lets you record or code a sequence of actions and replay them with…
- Excel VBA Function Parameters: Defaults, ByRef, and MoreIn Excel VBA, a standard function parameter is a named input declared in the function’s parentheses, such as…
- How to Assign a Macro to a Toggle Button in Excel 365In Excel for Microsoft 365 on Windows, an ActiveX Toggle Button runs VBA from its Click event: insert…
- VBA Loop Through Dictionary Keys: Examples and TipsTo loop through the keys in a VBA Scripting.Dictionary, use For Each with a Variant control variable: For…
- Macro Recorder Best Options for Excel and Windows PCsThe best macro recorder depends on what you want to automate: for repeated tasks inside Excel, start with…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.