'******************************************************************************************************************************************************
' ___ _ _______ __ _ ____ _ _ _______ ___ _ _ _ ___ _ _.
' // \\ /\\ // // \\ // // // // // // \\ // // // // \\ //| //
' //___// //__\ // //__// // // //__|| // // // // // // // // // | //
' // // \\ // // \\ // // // \\ // // // // // // // // // | //
'// // // // // // // //___ // \\ // \\__// //__// //___ \\__// // |//
'******************************************************************************************************************************************************
Option Explicit
Sub testx()
Dim R(1 To 4) As Range
Dim texte As String
Dim rng As Range
Dim RangeExcluded As Range
Dim KeepRange As Range
Cells.Interior.Color = xlNone
'for the example 4 ranges which intersect somewhere
Set R(1) = [D3:J9]
Set R(2) = [E1:E15]
Set R(3) = [B7:E7]
Set R(4) = [G1:G5]
'for the demo I put the ranges in color
R(1).Interior.Color = RGB(0, 200, 255)
R(2).Interior.Color = RGB(0, 255, 200)
R(3).Interior.Color = RGB(255, 200, 0)
R(4).Interior.Color = RGB(200, 255, 0)
'we group them all together with union function
Set rng = Union(R(1), R(2), R(3), R(4))
'I will find the ranges to be excluded with the GetRangeToExclude function
'by having the range excluded you can even use it in another context
Set RangeExcluded = GetRangeToExclude(rng)
'we keep the cells which do not intersect with others and which remain in the surface of the area(1)
Set KeepRange = GetKeepRange(rng, RangeExcluded, R(1)) 'on ne garde que c'elles qui sont dans la surface de r(1) sauf les intersections
'we keep the cells which do not intersect with others for all areas
'Set KeepRange = GetKeepRange(rng, RangeExcluded) 'on ne garde tout sauf les intersections
texte = "The ranges tested are : " & rng.Address(0, 0) & vbCrLf
texte = texte & "The ranges exclued are : " & RangeExcluded.Address(0, 0) & vbCrLf
texte = texte & "The guarded ranges are : " & KeepRange.Address(0, 0)
KeepRange.Select
MsgBox texte
End Sub
Sub tesz()
MsgBox GetRangeToExclude(Range("D3:J9;E1:E15;B7:E7;G1:G5")).Address
End Sub
Function GetRangeToExclude(rng As Range) As Range
'patricktoulon Developpez.com 18/06/2015
'fonction pour déterminer les plages qui se croisent dans un area ou un range
'le rng est une plage avec des areas
Dim RngEx As Range, A&, B&, Ri As Range
For A = 1 To rng.Areas.Count
For B = 1 To rng.Areas.Count
If B <> A Then
Set Ri = Intersect(rng.Areas(A), rng.Areas(B))
If Not Ri Is Nothing Then
If RngEx Is Nothing Then Set RngEx = Ri Else Set RngEx = Union(RngEx, Ri)
End If
End If
Next
Next
Set GetRangeToExclude = RngEx
End Function
Function GetKeepRange(R As Range, RngEx As Range, Optional RngRef As Range = Nothing) As Range
'patricktoulon Developpez.com 18/06/2015
'récupération des plages sans les intersections
Dim RngF As Range, area, cel As Range, X As Boolean
If Not RngRef Is Nothing Then Set R = R.Areas(1)
For Each cel In R.Cells
X = True
For Each area In RngEx.Areas
If Not Intersect(area, cel) Is Nothing Then X = False: Exit For
Next
If X Then If RngF Is Nothing Then Set RngF = cel Else Set RngF = Union(RngF, cel)
Next
Set GetKeepRange = RngF
End Function