Find Range Intersections without looping

chuckjitsu

New Member
Joined
Apr 24, 2015
Messages
48
Office Version
  1. 365
  2. 2016
Hello. Range A1:A5 is populated with range addresses as seen below. What I'm trying to do is see if another range address intersects any of those 5 range addresses and do this without looping. For example, let's say I wanted to check and see if range A7:A10 intersects any of the A1:A5 values below. I know I can loop through cells A1:A5 and compare their range values to range A7:A10 via INTERSECT to check for intersection. Is there a way to do this without looping through A1:A5, almost like using MATCH or FIND to look for intersections? Hopefully that makes sense. Let me know if more info is needed.

I don't really have any code to share relative to this as I just couldn't get my arms around potential syntax, if what I'm asking is even possible. Thanks in advance.


A1:B1
A1:A2
B1:C1
A2:A3
C1:D1
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Perhaps like this:
VBA Code:
Dim tx As String
tx = Join(Application.Transpose(Range("A1:A5")), ",")

    If Not Intersect(Range("A7:A10"), Range(tx)) Is Nothing Then
        MsgBox "YES"
    Else
        MsgBox "NO"
    End If
 
Upvote 0
Solution
Other approach:

VBA Code:
Sub Find_Intersections()
  Dim c As Range
  For Each c In Range("A1:A5")
    If Not Intersect(Range(c.Value), Range([C1].Value)) Is Nothing Then c.Offset(, 1).Value = "Yes"
  Next
End Sub

Result:
varios 16sep2021.xlsm
ABC
1A1:B1A7:A10
2A1:A2
3B1:C1
4A2:A3
5A10:A11Yes
Hoja4
 
Upvote 0
Perhaps like this:
VBA Code:
Dim tx As String
tx = Join(Application.Transpose(Range("A1:A5")), ",")

    If Not Intersect(Range("A7:A10"), Range(tx)) Is Nothing Then
        MsgBox "YES"
    Else
        MsgBox "NO"
    End If
Hi Akuini. Thanks for the response. When I ran your code, i got a 1004 error on this line: If Not Intersect(Range("A7:A10"), Range(tx)) Is Nothing Then
 
Upvote 0
Other approach:

VBA Code:
Sub Find_Intersections()
  Dim c As Range
  For Each c In Range("A1:A5")
    If Not Intersect(Range(c.Value), Range([C1].Value)) Is Nothing Then c.Offset(, 1).Value = "Yes"
  Next
End Sub

Result:
varios 16sep2021.xlsm
ABC
1A1:B1A7:A10
2A1:A2
3B1:C1
4A2:A3
5A10:A11Yes
Hoja4
Hi Dante. Thanks for the response. I was trying to see if this could be done without looping.
 
Upvote 0
Hi Akuini. Thanks for the response. When I ran your code, i got a 1004 error on this line: If Not Intersect(Range("A7:A10"), Range(tx)) Is Nothing Then
I ran this:
VBA Code:
Sub try100()

Dim tx As String
tx = Join(Application.Transpose(Range("A1:A5")), ",")

    If Not Intersect(Range("A7:A10"), Range(tx)) Is Nothing Then
        MsgBox "YES"
    Else
        MsgBox "NO"
    End If

End Sub

on your sample:
Book1
A
1A1:B1
2A1:A2
3B1:C1
4A2:A3
5C1:D1
Sheet1


The message box appeared (it said "NO").
So no error.
Please try it in a clean sheet and the same example first.
 
Upvote 0
If you have blank cells in the A1:A5 range try this mod to Akuini's code
VBA Code:
Dim tx As String
tx = Join(Application.Transpose(Range("A1:A5")), " ")
tx = Replace(Application.Trim(tx), " ", ",")

    If Not Intersect(Range("A7:A10"), Range(tx)) Is Nothing Then
        MsgBox "YES"
    Else
        MsgBox "NO"
    End If
 
Upvote 0
I ran this:
VBA Code:
Sub try100()

Dim tx As String
tx = Join(Application.Transpose(Range("A1:A5")), ",")

    If Not Intersect(Range("A7:A10"), Range(tx)) Is Nothing Then
        MsgBox "YES"
    Else
        MsgBox "NO"
    End If

End Sub

on your sample:
Book1
A
1A1:B1
2A1:A2
3B1:C1
4A2:A3
5C1:D1
Sheet1


The message box appeared (it said "NO").
So no error.
Please try it in a clean sheet and the same example first.

I ran this:
VBA Code:
Sub try100()

Dim tx As String
tx = Join(Application.Transpose(Range("A1:A5")), ",")

    If Not Intersect(Range("A7:A10"), Range(tx)) Is Nothing Then
        MsgBox "YES"
    Else
        MsgBox "NO"
    End If

End Sub

on your sample:
Book1
A
1A1:B1
2A1:A2
3B1:C1
4A2:A3
5C1:D1
Sheet1


The message box appeared (it said "NO").
So no error.
Please try it in a clean sheet and the same example first.
Hi Akuini. You're code worked. It was human error (me) that caused the error message the first time around, so my apologies there and thanks again for providing a solution.
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,545
Members
449,317
Latest member
chingiloum

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