Function NotIntersect(rngOne As Excel.Range, rngTwo As Excel.Range) As Excel.Range
'// written by Greg Truby
'// 26 July 2011
'// in response to forum thread
'// [URL]http://www.mrexcel.com/forum/showthread.php?t=567096[/URL]
Dim booUpdateStat As Boolean, _
rngISect As Excel.Range, _
rngNotISect As Excel.Range, _
rngNewOne As Excel.Range, _
rngNewTwo As Excel.Range, _
rngUnion As Excel.Range, _
wbNew As Excel.Workbook, _
wsNew As Excel.Worksheet, _
wsParent As Excel.Worksheet
booUpdateStat = Application.ScreenUpdating
Application.ScreenUpdating = False
Set wsParent = rngOne.Parent
Set wbNew = Workbooks.Add(Template:=XlWBATemplate.xlWBATWorksheet)
Set wsNew = wbNew.Worksheets(1)
Set rngISect = Application.Intersect(rngOne, rngTwo)
If rngISect Is Nothing Then
Set NotIntersect = Union(rngOne, rngTwo)
GoTo TimeToGo
End If
Set rngNewOne = wsNew.Range(rngOne.Address)
Set rngNewTwo = wsNew.Range(rngTwo.Address)
rngNewOne.Formula = "1"
rngNewTwo.Formula = "1"
wsNew.Range(rngISect.Address).ClearContents
Set rngUnion = Union(rngNewOne, rngNewTwo)
On Error Resume Next
Set rngNotISect = rngUnion.SpecialCells(xlCellTypeConstants, XlSpecialCellsValue.xlNumbers)
If rngNotISect Is Nothing Then
Set NotIntersect = Nothing
Else
Set NotIntersect = wsParent.Range(rngNotISect.Address)
End If
TimeToGo:
'¨¨¨¨¨¨¨¨
With Application
wbNew.Close False
.ScreenUpdating = booUpdateStat
End With
End Function
Sub test()
On Error Resume Next
Dim rngTest As Excel.Range, R1 As Excel.Range, R2 As Excel.Range
'// no overlap
Set R1 = Range("A1:B3")
Set R2 = Range("D5:F7")
R1.Interior.Color = vbBlue
R2.Interior.Color = vbYellow
If Not Application.Intersect(R1, R2) Is Nothing Then
Application.Intersect(R1, R2).Interior.Color = vbGreen
End If
Set rngTest = NotIntersect(R1, R2)
rngTest.Interior.Color = vbMagenta
MsgBox rngTest.Address, vbInformation
R1.Interior.Color = XlColorIndex.xlColorIndexNone
R2.Interior.Color = XlColorIndex.xlColorIndexNone
rngTest.Interior.Color = XlColorIndex.xlColorIndexNone
'// some overlap
Set R1 = Range("A1:D6")
Set R2 = Range("C3:F9")
R1.Interior.Color = vbBlue
R2.Interior.Color = vbYellow
If Not Application.Intersect(R1, R2) Is Nothing Then
Application.Intersect(R1, R2).Interior.Color = vbGreen
End If
Set rngTest = NotIntersect(R1, R2)
rngTest.Interior.Color = vbMagenta
MsgBox rngTest.Address, vbInformation
R1.Interior.Color = XlColorIndex.xlColorIndexNone
R2.Interior.Color = XlColorIndex.xlColorIndexNone
rngTest.Interior.Color = XlColorIndex.xlColorIndexNone
'// 2 inside 1
Set R1 = Range("A1:F10")
Set R2 = Range("C3:E7")
R1.Interior.Color = vbBlue
R2.Interior.Color = vbYellow
If Not Application.Intersect(R1, R2) Is Nothing Then
Application.Intersect(R1, R2).Interior.Color = vbGreen
End If
Set rngTest = NotIntersect(R1, R2)
rngTest.Interior.Color = vbMagenta
MsgBox rngTest.Address, vbInformation
R1.Interior.Color = XlColorIndex.xlColorIndexNone
R2.Interior.Color = XlColorIndex.xlColorIndexNone
rngTest.Interior.Color = XlColorIndex.xlColorIndexNone
'// 1 = 2
Set R1 = Range("A1:D6")
Set R2 = Range("A1:D6")
R1.Interior.Color = vbBlue
R2.Interior.Color = vbYellow
If Not Application.Intersect(R1, R2) Is Nothing Then
Application.Intersect(R1, R2).Interior.Color = vbGreen
End If
Set rngTest = NotIntersect(R1, R2)
R1.Interior.Color = XlColorIndex.xlColorIndexNone
If Err.Number <> 0 Then
MsgBox "R1 = R2 (NotIntersect is Nothing)", vbInformation
Else
MsgBox rngTest.Address, vbInformation
End If
End Sub