The phrase If Not Intersect(Target, Range) Is Nothing Then is the standard way in Excel VBA to check if a specific cell or range of cells has been modified. In simple terms, it asks: "Is the cell that just changed part of the area I care about?" If the answer is yes, the code inside the If-block executes. This is essential for creating interactive spreadsheets where changes in one cell automatically trigger calculations, data validation, or formatting changes in another part of the workbook.

What You'll Need

  • Microsoft Excel installed on a Windows or Mac computer.
  • The Developer tab enabled in the Excel Ribbon.
  • A basic understanding of how to open the Visual Basic Editor (VBE).
  • An existing worksheet where you want to monitor specific cell changes.

Step-by-Step

  1. Open the Visual Basic Editor: Open your Excel workbook. Press Alt + F11 on your keyboard to launch the VBA Editor.
  2. Locate the Correct Sheet Module: In the Project Explorer pane on the left, find the workbook you are currently working in. Do not place this code in a standard module (like Module1). Instead, double-click the specific sheet name (e.g., Sheet1 (Sheet1)) where you want the automation to occur. This is because Intersect is typically used within Worksheet Events.
  3. Select the Worksheet_Change Event: At the top of the code window, there are two dropdown menus. In the left dropdown, select Worksheet. In the right dropdown, select Change. Excel will automatically generate the shell for the Private Sub Worksheet_Change(ByVal Target As Range) procedure.
  4. Define Your Target Range: Decide which cells should trigger the macro. For example, if you only want the code to run when cells A1 through A10 are changed, your range is Range("A1:A10").
  5. Insert the Intersect Logic: Inside the Worksheet_Change sub, type the following line of code: If Not Intersect(Target, Range("A1:A10")) Is Nothing Then. This line tells VBA to look at the Target (the cell that was just edited) and see if it overlaps with your specified range.
  6. Add Your Action Code: Directly below the If statement, write the code you want to execute. For example, you might want to display a message box: MsgBox "You changed a monitored cell!".
  7. Close the If Block: End the logic by typing End If on a new line.
  8. Prevent Infinite Loops: If your macro changes a value in the sheet, it will trigger the Worksheet_Change event again, creating an infinite loop. To prevent this, add Application.EnableEvents = False immediately after the If statement, and Application.EnableEvents = True immediately before End If.
  9. Test the Macro: Return to your Excel sheet. Change a value in cell A1; the macro should run. Change a value in cell B1; nothing should happen.

Common Mistakes

One of the most frequent errors is placing the code in a standard Module instead of a Sheet Module. Because the Target variable is passed specifically by the Worksheet event, the code will not recognize Target if it is placed in Module1, resulting in a "Variable not defined" error.

Another common mistake is forgetting the Not and Is Nothing keywords. The Intersect function returns a Range object if there is an overlap, and Nothing if there is no overlap. Therefore, Intersect(Target, Range) Is Nothing means "the cells do NOT overlap." By adding Not at the beginning, you invert the logic to mean "the cells DO overlap." Writing If Intersect(Target, Range) Then will cause a type mismatch error because an object cannot be evaluated as a simple True/False boolean.

Users also frequently forget to disable events. If your code changes cell A2 when A1 is edited, and both are in your monitored range, the code will trigger itself repeatedly until Excel crashes or throws a "Stack Overflow" error. Always wrap your modifications in Application.EnableEvents = False and True.

Tips

To make your code more maintainable, avoid hard-coding range addresses like "A1:A10". Instead, use a Named Range in Excel (e.g., name the cells "InputZone"). In your VBA code, you can then use Range("InputZone"). This allows you to change the monitored area in the Excel interface without having to open the VBA editor and rewrite the code.

If you need to monitor multiple non-contiguous areas, you can comma-separate them within the range property, such as Range("A1:A10, C1:C10"). This ensures the macro triggers regardless of which of those two columns is edited.

For better performance in large sheets, consider using Target.CountLarge. If a user deletes a whole column or pastes a massive block of data, Target becomes a huge range. Checking If Target.CountLarge > 1 Then Exit Sub at the very top of your procedure can prevent the macro from trying to process thousands of cells at once, which would otherwise freeze the application.

Troubleshooting

If the macro simply stops working and does not trigger when you change a cell, the most likely cause is that Application.EnableEvents was set to False during a previous crash or error and was never set back to True. Because this is a global application setting, it stays false even if you restart the macro. To fix this, press Ctrl + G in the VBA editor to open the Immediate Window, type Application.EnableEvents = True, and press Enter.

If you receive a "Type Mismatch" error, double-check your syntax. Ensure you have both Not at the start and Is Nothing at the end. If you are using a variable for your range instead of a hard-coded string, ensure that variable has been properly Set using the Set keyword (e.g., Set myRange = Range("A1:A10")), as ranges are objects and cannot be assigned with a simple equals sign.

If the macro triggers too often or on the wrong cells, verify that your range definition is accurate. Remember that Intersect is inclusive; if your target range is A1:A10 and the user selects A5:A15 and hits delete, the intersection is A5:A10, which is Not Nothing, so the code will run.

Frequently Asked Questions

Why is it called "Not Intersect Is Nothing"?

The Intersect function returns a range object representing the overlapping area. If there is no overlap, it returns Nothing. To check if an overlap exists, we check if the result Is Nothing. To trigger the code when an overlap does exist, we use Not to flip the result of that check.

Can I use this with the Worksheet_Calculate event?

No. The Worksheet_Calculate event does not provide a Target range because a calculation can be triggered by any change anywhere in the workbook. Intersect requires a specific range to compare against, which is only available in the Worksheet_Change event.

Does this work if I change a cell via another macro?

Yes, by default, changes made by VBA will trigger the Worksheet_Change event. This is why disabling events with Application.EnableEvents = False is so important—otherwise, your macro will trigger itself in an infinite loop.

What happens if I select multiple cells and press Delete?

The Target variable will represent the entire selected range. If any part of that selected range overlaps with your monitored range, the Intersect function will return the overlapping portion, and the If Not...Is Nothing condition will be true, triggering the code.

Can I monitor a whole column instead of a specific range?

Yes. You can use Range("A:A") or Columns("A"). This is useful for sheets where the number of rows grows over time, ensuring that any new data entered in column A is always processed by the macro.