Excel VBA Not Intersect: How to Use Intersect and Not
In this article
To achieve a "not intersect" effect in Excel VBA, you wrap the Intersect function inside a Not operator. This allows you to execute code only when a specific cell or range is not part of a target area, which is most commonly used in the Worksheet_Change or Worksheet_SelectionChange events to prevent macros from running in certain parts of a sheet. For example, If Not Intersect(Target, Range("A1:A10")) Is Nothing Then checks if there is an overlap, but to do the opposite, you check if the intersection Is Nothing.
What You'll Need
- Microsoft Excel installed on a Windows or Mac PC.
- The Developer tab enabled in the Excel Ribbon.
- Basic familiarity with the Visual Basic Editor (VBE).
- A worksheet with data where you want to restrict or trigger automation.
Step-by-Step
- Open the Visual Basic Editor: Press
Alt + F11on your keyboard. This opens the VBE window where all your macros and event handlers are stored. - Locate the Worksheet Object: In the Project Explorer window on the left, find the workbook you are currently using. Double-click the specific sheet name (e.g.,
Sheet1 (Sheet1)) where you want the logic to apply. You must place this code in the Sheet module, not a standard Module, for event triggers to work. - Select the Event: From the left-hand dropdown menu at the top of the code window, select
Worksheet_Change. This automatically generates the subroutinePrivate Sub Worksheet_Change(ByVal Target As Range). TheTargetvariable represents the cell or range that was just modified. - Define Your Protected Range: Decide which range should be ignored by your macro. For this example, let's say you want your macro to run everywhere except in cells A1 through B10.
- Implement the Not Intersect Logic: Inside the subroutine, write an
Ifstatement using theIntersectfunction. To ensure the code runs only when the change happens outside of A1:B10, use the following syntax:
If Intersect(Target, Range("A1:B10")) Is Nothing Then
In VBA,Intersectreturns a Range object if there is an overlap andNothingif there is no overlap. By checking if the resultIs Nothing, you are effectively saying "If the changed cell is NOT in this range, then proceed." - Add Your Action Code: Place the code you want to execute between the
IfandEnd Ifstatements. For example, you might addMsgBox "You changed a cell outside the protected zone!". - Handle Potential Infinite Loops: If your macro changes values on the sheet, it will trigger the
Worksheet_Changeevent again, creating an infinite loop. To prevent this, wrap your action code withApplication.EnableEvents = Falseat the start andApplication.EnableEvents = Trueat the end. - Test the Logic: Return to your Excel sheet. Change a cell in range C1. The macro should trigger. Now, change a cell in range A1. The macro should not trigger.
Common Mistakes
One of the most frequent errors is confusing the logic of Is Nothing. In standard English, "Nothing" sounds negative, but in VBA's Intersect context, Is Nothing means "no overlap found." Therefore, if you want to run code when a user clicks outside a range, you use If Intersect(...) Is Nothing Then. If you want to run code when they click inside a range, you use If Not Intersect(...) Is Nothing Then.
Another common mistake is failing to define the range correctly. Using Range("A1:A10") is fine for a single sheet, but if your code is in a standard module rather than a sheet module, you must specify the sheet name, such as Sheets("Sheet1").Range("A1:A10"), otherwise VBA assumes the ActiveSheet, which can lead to errors if the user is on a different tab.
Many users also forget to handle multi-cell selections. The Target variable can be a single cell or a massive block of cells (like if a user deletes a whole column). If your logic depends on a single cell value, but the user selects a range, your code may crash with a "Type Mismatch" error. Always consider using If Target.CountLarge > 1 Then Exit Sub at the top of your event to avoid this.
Tips
To make your code more maintainable, avoid hard-coding range addresses like "A1:B10". Instead, use Named Ranges. In Excel, highlight your protected area and name it "ProtectedZone" in the Name Box. In VBA, you can then use Intersect(Target, Range("ProtectedZone")). This allows you to resize the protected area in the Excel interface without ever having to open the VBA editor to change the code.
For more complex scenarios, you can combine multiple ranges using the Union function. If you have several disconnected areas that should be ignored, you can create a variable for the combined range: Dim ignoreRange As Range: Set ignoreRange = Union(Range("A1:B10"), Range("D1:E10")). Then, use If Intersect(Target, ignoreRange) Is Nothing Then.
If you are using this logic to create a "User Form" feel where only certain cells are editable, consider combining this VBA approach with Excel's built-in Protect Sheet feature. You can unlock specific cells and protect the rest of the sheet, using the VBA Intersect logic only for advanced validation or automated calculations that protection cannot handle.
Troubleshooting
If your macro is not triggering at all, the most likely culprit is that Application.EnableEvents has been set to False and was never turned back on. This often happens if a previous run of the macro crashed before reaching the True line. To fix this, press Ctrl + G in the VBE to open the Immediate Window, type Application.EnableEvents = True, and press Enter.
If the macro is triggering when it should be ignored (or vice versa), double-check your Is Nothing logic. Remember: Intersect(...) Is Nothing means "Not Overlapping." If you find the logic is inverted, simply remove or add the Not operator or change Is Nothing to Not Is Nothing.
If you receive a "Global or Interface not defined" error, ensure you are using the correct object types. Target must be a Range object. If you have declared Target as a different type in a custom subroutine, the Intersect function will fail because it requires two Range objects as arguments.
Frequently Asked Questions
Can I use Not Intersect with the SelectionChange event?
Yes. The logic is identical. Worksheet_SelectionChange triggers whenever the user moves the cursor. Using If Intersect(Target, Range("A1")) Is Nothing Then will execute code whenever the user selects any cell except A1.
What is the difference between Intersect and Union?
Intersect finds the overlapping area between two ranges. If they don't overlap, it returns Nothing. Union combines two or more ranges into one single range object, regardless of whether they overlap or are far apart.
Does this work across multiple worksheets?
The event-based code must be placed in the specific sheet module to work automatically. If you want a "not intersect" rule to apply to all sheets in a workbook, you must place the code in the ThisWorkbook module using the Workbook_SheetChange event.
Why does my code run multiple times when I delete a range?
When you delete or change a large block of cells, the Target range becomes that entire block. If your code loops through the Target cells, it will run for every cell in that block. Use Target.CountLarge to limit the execution to single-cell changes.
Can I use this to prevent users from clicking certain cells?
While you can use SelectionChange to move the cursor away from a cell using Range("B1").Select, it can be jarring for the user. It is generally better to use the Protect Sheet feature to lock cells, using VBA only for dynamic logic.