Intersect, Target With Multiple Ranges

dbmathis

Well-known Member
Joined
Sep 22, 2002
Messages
1,064
Hi All,

How should I alter this:

Code:
If Intersect(Target, Range("C14:D59")) Is Nothing Then Exit Sub

to work for multiple ranges?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I would use Union to for the multiple ranges you are writing about. Then, the Insersect can just be specified on Target (as now) and the union of the others. This will probably the most clean way of writing the code.
 
Upvote 0
I would use Union to for the multiple ranges you are writing about. Then, the Insersect can just be specified on Target (as now) and the union of the others. This will probably the most clean way of writing the code.

Why do you need to use the Union method?
 
Upvote 0
How should I alter this:

Code:
If Intersect(Target, Range("C14:D59")) Is Nothing Then Exit Sub

to work for multiple ranges?
Just use a comma separated list inside the Range call. For example, here is a 3-area test using this method.....

Code:
If Intersect(Target, Range("A3:C5,D4:F4,H6:H9")) Is Nothing Then Exit Sub
 
Upvote 0
Why do you need to use the Union method?

Hello

As I indicated, that is probably the most clean way to write it. I did not say that you have to use it.

It might be more convenient (comparing with the solution by Rick) because I think it can be read easier. It will also allow for some loops (for instance, every other column could be easily done in a loop) and a Union, rather than a hardcoded list.

With Union we could add some "error handling", look at this topic of yesterday:
http://www.mrexcel.com/forum/showpost.php?p=2831125

I am not sure, but maybe there is a limit to the string length in Rick's code example - what is in between the Range(). Again, not sure of that.
 
Upvote 0
I am not sure, but maybe there is a limit to the string length in Rick's code example - what is in between the Range(). Again, not sure of that.
I am doing this from memory (not always a good thing to do;)), but I think there is a 255 character limit for the text string argument to the Range property call.
 
Upvote 0
... the most clean way to write it .... I think it can be read easier ..... every other column could be easily done in a loop ......
With Union we could add some "error handling"

Can't think why you say it's cleaner(?) to write and easier to read.

Also, building a range via looping does not have to be done using Union, nor does error handling depend upon using Union.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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