Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA Intersect

I found this code online and apparently it works for other people but not me? I don't know where it's wrong. I made a simple example and have my Range1 and Range 2 to be certain cells in excel, enter image description here

Also, I would like to know if there is a way to return the intersections, if it can. Thanks in advance!

Function InRange(Range1 As Range, Range2 As Range) As Boolean
    Set intersectRange = Application.Intersect(Range1, Range2)
    If intersectRange Is Nothing Then
        InRange = False
    Else
        InRange = True
    End If
End Function
like image 379
Ogre Magi Avatar asked Mar 26 '26 19:03

Ogre Magi


1 Answers

It seems to me that you are expecting Intersect to check whether the two ranges have cells with the same values? Is that the point from having the value "a" in both S15 and T15?

If so, then this is why you are not getting what you expect. The Intersect function returns a range that is the "overlap" of the two ranges, regardless of the values inside each.

As this is my first post on SE, I hope this helps :)

like image 200
A.S.H Avatar answered Mar 29 '26 16:03

A.S.H