One range inside another, Intersect

N

Neal Zimm

After I had the same situation, and reading some posts about checking to see
if the cells in one range were contained in another range, I wrote the
function below.

It returns the boolean True or False value, and optionally, if range A is
not entirely within range B, the first failing cell is returned.

Public Function bRngAinRngBF(RngA As Range, RngB As Range, _
Optional FailedRng As Range = Nothing) As Boolean
'True if every cell in RngA is in RngB. > 1 Area is OK in either.
'When False, the 1st cell in RngA not found in RngB is returned in FailedRng.

Dim CellA As Range
Dim IxB As Integer, IxA As Integer


Set FailedRng = Nothing
If RngA Is Nothing Or RngB Is Nothing Then Exit Function

For IxA = 1 To RngA.Areas.Count
For Each CellA In RngA.Areas(IxA)

For IxB = 1 To RngB.Areas.Count
If Not Intersect(CellA, RngB.Areas(IxB)) Is Nothing Then Exit For
'found
Next IxB

If IxB > RngB.Areas.Count Then
Set FailedRng = CellA
Exit Function
End If

Next CellA
Next IxA

bRngAinRngBF = True ' Both loops complete, function is true

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top