VBA - Determine if one range is within another?

mothra

New Member
Joined
Feb 13, 2008
Messages
8
What's the best way to find out if one range is within another? For example...

is "A6:A10" within "A1:A20" ......(yes)
is "B8" within "A1:B4" ...............(no)


Hoping to do this without loops.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
I think it depends on whether you can accept knowing that at least one cell in each range overlaps or if only knowing if all cells in one of the ranges is contained in the other will do. The former:

Code:
If Not Intersect(Range1,Range2) Is Nothing Then
  'they overlap at least partially
Else
  'They don't overlap at all
End If
 
Upvote 0
Code:
Sub test()
If Not Intersect(Range("A6:A10"), Range("A1:A20")) Is Nothing Then
    MsgBox ("Range A6:A10 intersects Range A1:A20")
End If

If Intersect(Range("B8"), Range("A1:B4")) Is Nothing Then
    MsgBox ("Range B8 does not intersects Range A1:B4")
End If

End Sub

You have to be careful with the If or If Not's - as above. If the ranges intersect, the returned object is not Nothing ...

Regards.
 
Upvote 0
For whole overlaps you could perhaps use:

Code:
Set rOverlap = Intersect(Range1,Range2)
If Not rOverlap Is Nothing Then  'ie is there some overlap?
  If rOverlap.Count = Range1.Count Or rOverlap.Count = Range2.Count Then
    'one range is fully contained within the second range
  Else
    'ranges only partially overlap
  End If
Else
  'Ranges don'toverlap at all
End If

The above does assume that Range1 and Range2 are both contiguous ranges.
 
Upvote 0
Hi

Another one, with all the cases

Code:
    If Intersect(r1, r2) Is Nothing Then
        MsgBox "r1 and r2 do not intersect"
    ElseIf r1.Address = r2.Address Then
        MsgBox "r1 and r2 overlap completely"
    ElseIf Intersect(r1, r2).Address = r2.Address Then
        MsgBox "r2 is completely contained in r1"
    ElseIf Intersect(r1, r2).Address = r1.Address Then
        MsgBox "r1 is completely contained in r2"
    ElseIf Not Intersect(r1, r2) Is Nothing Then
        MsgBox "r1 and r2 intersect"
    End If

The last condition is not necessary.
 
Upvote 0

Forum statistics

Threads
1,222,609
Messages
6,167,046
Members
452,093
Latest member
JamesFromAustin

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