IF NOT INTERSECT for several non contiguous columns

PATSYS

Well-known Member
Joined
Mar 12, 2006
Messages
1,750
Hi all,

I am composing a worksheet_change code.

How do I express in VBA so that only changes in named ranges:

CWC
PRO
UPL

will trigger the macro?

I used named ranges because the users are inserting columns in the data table.


It is kinda like:

Code:
IF NOT INTERSECT(TARGET, RANGE("CWC"))  OR IF NOT INTERSECT(TARGET, RANGE("PRO")) OR IF NOT INTERSECT(TARGET, RANGE("UPL")) THEN

But above is a wrong syntax.

Thanks
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Possibly

Code:
If Not Intersect(Target, Union(Range("CWC"), Range("PRO"), Range("UPL"))) Then
 
Upvote 0
Possibly

Code:
If Not Intersect(Target, Union(Range("CWC"), Range("PRO"), Range("UPL"))) Then

Hi Peter,

Thanks for your reply.

However above code doesn't work. When I enter a value in either of the ranges, it did not fire the code.

It appears that the above expression does not only test intersect of TARGET vs the other ranges (CWC, PRO, UPL) but also the intersection of the ranges (CWC, PRO, UPL) against each other. This is just my opinion.

Can you assist further?

Thanks
 
Upvote 0
Hi Peter, sorry I missed something in your entire post.

Your code actually works perfectly.

Thank you very much.
 
Upvote 0
Possibly
Code:
If Not Intersect(Target, Union(Range("CWC"), Range("PRO"), Range("UPL"))) Then

First off, it looks like you accidentally omitted the "Is Nothing" part of your statement.

Second, you can omit the Union and form the range directly...
Code:
If Not Intersect(Target, Range("CWC,PRO,UPL")) Is Nothing Then

And since the argument to Range is a String, you have the flexibility of forming the range dynamically within your code if need be.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,291
Members
452,902
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top