Private Function GroupCollectionRNG(ByRef RangeCollection As VBA.Collection)
Dim sheet As Worksheet
Dim MergeCount As Long
RangeCollectionCount = RangeCollection.Count
For i = 1 To RangeCollectionCount ' Separates Column Letter and Row number to sort by 2 columns
RNGtoCompare = RangeCollection(i)
ThisWorkbook.Worksheets("Unique Ranges").Range("A" & i).Value = RNGtoCompare
ThisWorkbook.Worksheets("Unique Ranges").Range("B" & i).Value = Split(Columns(Range(RNGtoCompare).Column).Address(, False), ":")(1)
ThisWorkbook.Worksheets("Unique Ranges").Range("C" & i).Value = Range(RNGtoCompare).Row
Next i
If RangeCollectionCount > 0 Then
ThisWorkbook.Worksheets("Unique Ranges").Range("A1:C" & RangeCollectionCount).sort Key1:=Range("B1"), Order1:=xlAscending, Key2:=Range("C1"), Order1:=xlAscending, Header:=xlNo ' Sorts by Column Letter and Row number
End If
' Empty collection
For x = 1 To RangeCollectionCount
RangeCollection.Remove (1)
Next x
For i = 1 To RangeCollectionCount ' Read all Values into RangeCollection after sorting
RNGvalue = ThisWorkbook.Worksheets("Unique Ranges").Range("A" & i).Value
RangeCollection.Add RNGvalue
Next i
MergeCount = 0
ThisWorkbook.Worksheets("Unique Ranges").Range("A1:C10000").ClearContents
' Loop to compare previous Range Column and next Range Column to see to see if they are adjacent. Remember this is done on same Row so comparison is only for horizontal comparison.
' If they are contiguous, join and compare next Range until the end of SameIntColor Collection. Once joined, remove from Collection and decrease K end of Loop
' BEGINNING of HORIZONTAL GROUPING
On Error Resume Next
For j = 1 To RangeCollectionCount - MergeCount ' Loop to Set RNG to that rest of collection would compare to. This Loop only merges Horizontal Ranges together.
If InStr(RangeCollection(j), ":") > 0 Or InStr(RNGtoCompare, ":") > 0 Then
BaseRNGisMerged = True
Else
RNGtoCompare = RangeCollection(j) ' Sets Value that rest of collection would compare to
BaseRNGisMerged = False
End If
For k = 1 To RangeCollectionCount - MergeCount ' Loop of compare all Ranges in Collection
If Not RNGtoCompare = RangeCollection(k) Then ' This conditional statement ensures comparision to itself does not occur
CurrentRNGValue = RangeCollection(k)
If InStr(CurrentRNGValue, ":") > 0 Then
LoopRNGisMerged = True
Else
LoopRNGisMerged = False
End If
If InStr(RNGtoCompare, ":") > 0 Then
BaseRNGisMerged = True
Else
BaseRNGisMerged = False
End If
If BaseRNGisMerged = False And LoopRNGisMerged = False Then ' Checks to see if Base and Current Range is a Merged Range *** FOR UNMERGED RANGES ***
BaseLetter = Split(Columns(Range(RNGtoCompare).Column).Address(, False), ":")(1)
BaseColNum = Range(BaseLetter & 1).Column
BaseRow = Range(RNGtoCompare).Row
CurrentLetter = Split(Columns(Range(CurrentRNGValue).Column).Address(, False), ":")(1)
CurrentColNum = Range(CurrentLetter & 1).Column
CurrentRow = Range(CurrentRNGValue).Row
If BaseRow = CurrentRow And BaseColNum + 1 = CurrentColNum Then '***** CurrentCol is to the Right of Base Column *****
MergedRNG = BaseLetter & BaseRow & ":" & CurrentLetter & BaseRow
RangeCollection.Add MergedRNG
BaseRNGisMerged = True
MergeCount = MergeCount + 1
LoopCount = 0
For x = 1 To RangeCollection.Count
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare And Not IsNull(RangeCollection(x)) Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollectionCount = RangeCollection.Count - MergeCount
j = j - 1
k = RangeCollection.Count
Exit For
' NEED TO WORK ON CODE TO COMPARE ALL OTHER NON-MERGED VALUES WITH RECENTLY MERGED RANGE
ElseIf BaseRow = CurrentRow And BaseColNum - 1 = CurrentColNum Then '***** CurrentCol is to the Left of Base Column *****
MergedRNG = CurrentLetter & BaseRow & ":" & BaseLetter & BaseRow
BaseRNGisMerged = True
RangeCollection.Add MergedRNG
MergeCount = MergeCount + 1
LoopCount = 0
For x = 1 To RangeCollection.Count
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RNGtoCompare = RangeCollection(1)
RangeCollectionCount = RangeCollection.Count - MergeCount
k = RangeCollection.Count
j = j - 1
Exit For
End If
ElseIf BaseRNGisMerged = True And Len(RNGtoCompare) - Len(Replace(RNGtoCompare, "/", "")) = 0 And LoopRNGisMerged = False Then ' Base Range is Merged, Loop Range is NOT Merged
HyphenLoc = InStr(RNGtoCompare, ":")
FirstHalfBaseRNG = Left(RNGtoCompare, HyphenLoc - 1)
FirstHalfBaseLetter = Split(Columns(Range(FirstHalfBaseRNG).Column).Address(, False), ":")(1)
FirstHalfBaseColNum = Range(FirstHalfBaseLetter & 1).Column
SecondHalfBaseRNG = Right(RNGtoCompare, Len(RNGtoCompare) - HyphenLoc)
SecondHalfBaseLetter = Split(Columns(Range(SecondHalfBaseRNG).Column).Address(, False), ":")(1)
SecondHalfBaseColNum = Range(SecondHalfBaseLetter & 1).Column
BaseRow = Range(SecondHalfBaseRNG).Row
CurrentLetter = Split(Columns(Range(CurrentRNGValue).Column).Address(, False), ":")(1)
CurrentColNum = Range(CurrentLetter & 1).Column
CurrentRow = Range(CurrentRNGValue).Row
If BaseRow = CurrentRow And SecondHalfBaseColNum + 1 = CurrentColNum Then '***** CurrentCol is to the Right of Base Column *****
MergedRNG = FirstHalfBaseLetter & BaseRow & ":" & CurrentLetter & BaseRow
BaseRNGisMerged = True
MergeCount = MergeCount + 1
LoopCount = 0
For x = 1 To RangeCollection.Count
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
RNGtoCompare = RangeCollection(1)
RangeCollectionCount = RangeCollection.Count - MergeCount
k = RangeCollection.Count
j = j - 1
Exit For
ElseIf BaseRow = CurrentRow And FirstHalfColNum - 1 = CurrentColNum Then '***** CurrentCol is to the Left of Base Column *****
MergedRNG = CurrentLetter & BaseRow & ":" & SecondHalfBaseLetter & BaseRow
BaseRNGisMerged = True
MergeCount = MergeCount + 1
LoopCount = 0
For x = 1 To RangeCollection.Count
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
RNGtoCompare = RangeCollection(1)
j = j - 1
k = RangeCollection.Count
RangeCollectionCount = RangeCollection.Count - MergeCount
Exit For
End If
ElseIf BaseRNGisMerged = False And LoopRNGisMerged = True And Len(CurrentRNGValue) - Len(Replace(CurrentRNGValue, "/", "")) = 1 Then ' Base Range is NOT merged, Loop Range is Merged
BaseLetter = Split(Columns(Range(RNGtoCompare).Column).Address(, False), ":")(1)
BaseColNum = Range(BaseLetter & 1).Column
BaseRow = Range(RNGtoCompare).Row
HyphenLoc = InStr(CurrentRNGValue, ":")
FirstHalfCurrentRNG = Left(CurrentRNGValue, HyphenLoc - 1)
FirstHalfCurrentLetter = Split(Columns(Range(FirstHalfCurrentRNG).Column).Address(, False), ":")(1)
FirstHalfCurrentColNum = Range(FirstHalfCurrentLetter & 1).Column
SecondHalfCurrentRNG = Right(CurrentRNGValue, Len(CurrentRNGValue) - HyphenLoc)
SecondHalfCurrentLetter = Split(Columns(Range(SecondHalfCurrentRNG).Column).Address(, False), ":")(1)
SecondHalfCurrentColNum = Range(SecondHalfCurrentLetter & 1).Column
CurrentRow = Range(SecondHalfCurrentRNG).Row
If BaseRow = CurrentRow And BaseColNum + 1 = FirstHalfCurrentColNum Then '***** CurrentCol is to the Right of Base Column *****
MergedRNG = BaseLetter & BaseRow & ":" & SecondHalfCurrentLetter & BaseRow
BaseRNGisMerged = True
MergeCount = MergeCount + 1
LoopCount = 0
For x = 1 To RangeCollection.Count
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
RNGtoCompare = RangeCollection(1)
RangeCollectionCount = RangeCollection.Count - MergeCount
k = RangeCollection.Count
j = j - 1
Exit For
ElseIf BaseRow = CurrentRow And BaseColNum - 1 = SecondHalfCurrentColNum Then '***** CurrentCol is to the Left of Base Column *****
MergedRNG = FirstHalfCurrentLetter & BaseRow & ":" & BaseLetter & BaseRow
BaseRNGisMerged = True
MergeCount = MergeCount + 1
LoopCount = 0
For x = 1 To RangeCollection.Count
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
RNGtoCompare = RangeCollection(1)
If k = RangeCollectionCount And RangeCollection(k) = RNGtoCompare Then
RNGtoCompare = RangeCollection(1)
End If
RangeCollectionCount = RangeCollection.Count - MergeCount
k = RangeCollection.Count
j = j - 1
Exit For
End If
End If
End If
If RNGtoCompare = RangeCollection(RangeCollection.Count - 1) And RangeCollection.Count = k Then
RNGtoCompare = RangeCollection(1)
Exit For
ElseIf j = RangeCollection.Count - 1 And k = RangeCollection.Count Then
Exit For
ElseIf j = RangeCollection.Count And k = RangeCollection.Count Then
Exit For
End If
end if
Next k
Next j
' END of HORIZONTAL GROUPING
' BEGINNING of VERTICAL GROUPING
RangeCollectionCount = RangeCollection.Count
RNGtoCompare = ""
CurrentRNGValue = ""
MergedRNG = ""
MergeCount = 0
If RangeCollection.Count > 2 Then
For m = 1 To RangeCollectionCount
RNGtoCompare = RangeCollection(m)
If m > RangeCollection.Count Then
Exit For
End If
If InStr(RangeCollection(m), ":") > 0 Or InStr(RNGtoCompare, ":") > 0 Then
BaseRNGisMerged = True
Else
RNGtoCompare = RangeCollection(m) ' Sets Value that rest of collection would compare to
BaseRNGisMerged = False
End If
If RNGtoCompare = "" Then
RNGtoCompare = RangeCollection(1)
End If
For n = 1 To RangeCollection.Count - MergeCount
If Not RNGtoCompare = RangeCollection(n) Then ' This conditional statement ensures comparision to itself does not occur
CurrentRNGValue = RangeCollection(n)
If InStr(CurrentRNGValue, ":") > 0 Then
LoopRNGisMerged = True
Else
LoopRNGisMerged = False
End If
If InStr(RNGtoCompare, ":") > 0 Then
BaseRNGisMerged = True
Else
BaseRNGisMerged = False
End If
Else
GoTo Endn
End If
' START OF LOOP TO CHECK FOR ALL MERGED ENTRIES
If BaseRNGisMerged = True And LoopRNGisMerged = True Then ' Checks to see if Base and Current Range is a Merged Range *** FOR MERGED RANGES ***
HyphenLoc = InStr(RNGtoCompare, ":")
FirstHalfBaseRNG = Left(RNGtoCompare, HyphenLoc - 1)
FirstHalfBaseLetter = Split(Columns(Range(FirstHalfBaseRNG).Column).Address(, False), ":")(1)
FirstHalfBaseColNum = Range(FirstHalfBaseLetter & 1).Column
SecondHalfBaseRNG = Right(RNGtoCompare, Len(RNGtoCompare) - HyphenLoc)
SecondHalfBaseLetter = Split(Columns(Range(SecondHalfBaseRNG).Column).Address(, False), ":")(1)
SecondHalfBaseColNum = Range(SecondHalfBaseLetter & 1).Column
FirstHalfBaseRow = Range(FirstHalfBaseRNG).Row
SecondHalfBaseRow = Range(SecondHalfBaseRNG).Row
HyphenLoc = InStr(CurrentRNGValue, ":")
FirstHalfCurrentRNG = Left(CurrentRNGValue, HyphenLoc - 1)
FirstHalfCurrentLetter = Split(Columns(Range(FirstHalfCurrentRNG).Column).Address(, False), ":")(1)
FirstHalfCurrentColNum = Range(FirstHalfCurrentLetter & 1).Column
SecondHalfCurrentRNG = Right(CurrentRNGValue, Len(CurrentRNGValue) - HyphenLoc)
SecondHalfCurrentLetter = Split(Columns(Range(SecondHalfCurrentRNG).Column).Address(, False), ":")(1)
SecondHalfCurrentColNum = Range(SecondHalfCurrentLetter & 1).Column
FirstHalfCurrentRow = Range(FirstHalfCurrentRNG).Row
SecondHalfCurrentRow = Range(SecondHalfCurrentRNG).Row
If FirstHalfBaseLetter = FirstHalfCurrentLetter And SecondHalfBaseLetter = SecondHalfCurrentLetter And SecondHalfBaseRow + 1 = FirstHalfCurrentRow Then ' Checks all Base and Current Merged Ranges
MergedRNG = FirstHalfBaseLetter & FirstHalfBaseRow & ":" & SecondHalfCurrentLetter & SecondHalfCurrentRow
BaseRNGisMerged = True
LoopCount = 0
For x = 1 To RangeCollection.Count - MergeCount
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
RNGtoCompare = MergedRNG
RangeCollectionCount = RangeCollection.Count
m = m - 1
If RNGtoCompare = RangeCollection(RangeCollection.Count) Then
RNGtoCompare = RangeCollection(m)
ThisWorkbook.Worksheets("Unique Ranges").Range("B1:B10000").Interior.Color = RGB(255, 255, 255)
ThisWorkbook.Worksheets("Unique Ranges").Range("B" & m).Interior.Color = RGB(0, 255, 0)
Exit For
End If
GoTo Endm
ElseIf FirstHalfBaseLetter = FirstHalfCurrentLetter And SecondHalfBaseLetter = SecondHalfCurrentLetter And FirstHalfBaseRow - 1 = SecondHalfCurrentRow Then
MergedRNG = FirstHalfCurrentLetter & FirstHalfCurrentRow & ":" & SecondHalfCurrentLetter & SecondHalfBaseRow
BaseRNGisMerged = True
LoopCount = 0
For x = 1 To RangeCollection.Count - MergeCount
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
RNGtoCompare = MergedRNG
RangeCollectionCount = RangeCollection.Count
m = m - 1
If RNGtoCompare = RangeCollection(RangeCollection.Count) Then
RNGtoCompare = RangeCollection(m)
ThisWorkbook.Worksheets("Unique Ranges").Range("B1:B10000").Interior.Color = RGB(255, 255, 255)
ThisWorkbook.Worksheets("Unique Ranges").Range("B" & m).Interior.Color = RGB(0, 255, 0)
Exit For
End If
GoTo Endm
End If
ElseIf BaseRNGisMerged = True And LoopRNGisMerged = False Then ' Base if Merged, Loop is not Merged
HyphenLoc = InStr(RNGtoCompare, ":")
FirstHalfBaseRNG = Left(RNGtoCompare, HyphenLoc - 1)
FirstHalfBaseLetter = Split(Columns(Range(FirstHalfBaseRNG).Column).Address(, False), ":")(1)
FirstHalfBaseColNum = Range(FirstHalfBaseLetter & 1).Column
SecondHalfBaseRNG = Right(RNGtoCompare, Len(RNGtoCompare) - HyphenLoc)
SecondHalfBaseLetter = Split(Columns(Range(SecondHalfBaseRNG).Column).Address(, False), ":")(1)
SecondHalfBaseColNum = Range(SecondHalfBaseLetter & 1).Column
FirstHalfBaseRow = Range(FirstHalfBaseRNG).Row
SecondHalfBaseRow = Range(SecondHalfBaseRNG).Row
CurrentLetter = Split(Columns(Range(CurrentRNGValue).Column).Address(, False), ":")(1)
CurrentColNum = Range(CurrentLetter & 1).Column
CurrentRow = Range(CurrentRNGValue).Row
If FirstHalfBaseLetter = SecondHalfBaseLetter And FirstHalfBaseLetter = CurrentLetter And FirstHalfBaseRow - 1 = CurrentRow Then
MergedRNG = CurrentLetter & CurrentRow & ":" & CurrentLetter & SecondHalfBaseRow
BaseRNGisMerged = True
LoopCount = 0
For x = 1 To RangeCollection.Count - MergeCount
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
RNGtoCompare = MergedRNG
RangeCollectionCount = RangeCollection.Count
m = m - 1
If RNGtoCompare = RangeCollection(RangeCollection.Count) Then
RNGtoCompare = RangeCollection(m)
ThisWorkbook.Worksheets("Unique Ranges").Range("B1:B10000").Interior.Color = RGB(255, 255, 255)
ThisWorkbook.Worksheets("Unique Ranges").Range("B" & m).Interior.Color = RGB(0, 255, 0)
Exit For
End If
GoTo Endm
ElseIf FirstHalfBaseLetter = SecondHalfBaseLetter And FirstHalfBaseLetter = CurrentLetter And SecondHalfBaseRow + 1 = CurrentRow Then
MergedRNG = CurrentLetter & FirstHalfBaseRow & ":" & CurrentLetter & CurrentRow
BaseRNGisMerged = True
MergeCount = MergeCount + 1
LoopCount = 0
For x = 1 To RangeCollection.Count
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
RNGtoCompare = MergedRNG
RangeCollectionCount = RangeCollection.Count
m = m - 1
If RNGtoCompare = RangeCollection(RangeCollection.Count) Then
RNGtoCompare = RangeCollection(m)
ThisWorkbook.Worksheets("Unique Ranges").Range("B1:B10000").Interior.Color = RGB(255, 255, 255)
ThisWorkbook.Worksheets("Unique Ranges").Range("B" & m).Interior.Color = RGB(0, 255, 0)
Exit For
End If
GoTo Endm
End If
ElseIf BaseRNGisMerged = False And LoopRNGisMerged = False Then ' Both are Unmerged
BaseLetter = Split(Columns(Range(RNGtoCompare).Column).Address(, False), ":")(1)
BaseColNum = Range(BaseLetter & 1).Column
BaseRow = Range(RNGtoCompare).Row
CurrentLetter = Split(Columns(Range(CurrentRNGValue).Column).Address(, False), ":")(1)
CurrentColNum = Range(CurrentLetter & 1).Column
CurrentRow = Range(CurrentRNGValue).Row
If BaseLetter = CurrentLetter And BaseRow + 1 = CurrentRow Then
MergedRNG = BaseLetter & BaseRow & ":" & BaseLetter & CurrentRow
BaseRNGisMerged = True
MergeCount = MergeCount + 1
LoopCount = 0
For x = 1 To RangeCollection.Count - MergeCount
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
RNGtoCompare = MergedRNG
RangeCollectionCount = RangeCollection.Count
If RNGtoCompare = RangeCollection(RangeCollection.Count) Then
RNGtoCompare = RangeCollection(m)
ThisWorkbook.Worksheets("Unique Ranges").Range("B1:B10000").Interior.Color = RGB(255, 255, 255)
ThisWorkbook.Worksheets("Unique Ranges").Range("B" & m).Interior.Color = RGB(0, 255, 0)
Exit For
End If
m = m - 1
GoTo Endm
ElseIf BaseLetter = CurrentLetter And BaseRow - 1 = CurrentRow Then
MergedRNG = BaseLetter & CurrentRow & ":" & BaseLetter & BaseRow
BaseRNGisMerged = True
LoopCount = 0
For x = 1 To RangeCollection.Count
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
RNGtoCompare = MergedRNG
RangeCollectionCount = RangeCollection.Count - MergeCount
m = m - 1
If RNGtoCompare = RangeCollection(RangeCollection.Count) Then
RNGtoCompare = RangeCollection(m)
ThisWorkbook.Worksheets("Unique Ranges").Range("B1:B10000").Interior.Color = RGB(255, 255, 255)
ThisWorkbook.Worksheets("Unique Ranges").Range("B" & m).Interior.Color = RGB(0, 255, 0)
Exit For
End If
GoTo Endm
End If
End If
If RNGtoCompare = RangeCollection(RangeCollection.Count) Then
RNGtoCompare = RangeCollection(m)
Exit For
End If
Endn:
Next n
Endm:
Next m
ElseIf RangeCollection.Count = 2 Then
RNGtoCompare = RangeCollection(1)
CurrentRNGValue = RangeCollection(2)
If InStr(CurrentRNGValue, ":") > 0 Then
LoopRNGisMerged = True
Else
LoopRNGisMerged = False
End If
If InStr(RNGtoCompare, ":") > 0 Then
BaseRNGisMerged = True
Else
BaseRNGisMerged = False
End If
If BaseRNGisMerged = True And LoopRNGisMerged = True Then
HyphenLoc = InStr(RNGtoCompare, ":")
FirstHalfBaseRNG = Left(RNGtoCompare, HyphenLoc - 1)
FirstHalfBaseLetter = Split(Columns(Range(FirstHalfBaseRNG).Column).Address(, False), ":")(1)
FirstHalfBaseColNum = Range(FirstHalfBaseLetter & 1).Column
SecondHalfBaseRNG = Right(RNGtoCompare, Len(RNGtoCompare) - HyphenLoc)
SecondHalfBaseLetter = Split(Columns(Range(SecondHalfBaseRNG).Column).Address(, False), ":")(1)
SecondHalfBaseColNum = Range(SecondHalfBaseLetter & 1).Column
FirstHalfBaseRow = Range(FirstHalfBaseRNG).Row
SecondHalfBaseRow = Range(SecondHalfBaseRNG).Row
HyphenLoc = InStr(CurrentRNGValue, ":")
FirstHalfCurrentRNG = Left(CurrentRNGValue, HyphenLoc - 1)
FirstHalfCurrentLetter = Split(Columns(Range(FirstHalfCurrentRNG).Column).Address(, False), ":")(1)
FirstHalfCurrentColNum = Range(FirstHalfCurrentLetter & 1).Column
SecondHalfCurrentRNG = Right(CurrentRNGValue, Len(CurrentRNGValue) - HyphenLoc)
SecondHalfCurrentLetter = Split(Columns(Range(SecondHalfCurrentRNG).Column).Address(, False), ":")(1)
SecondHalfCurrentColNum = Range(SecondHalfCurrentLetter & 1).Column
FirstHalfCurrentRow = Range(FirstHalfCurrentRNG).Row
SecondHalfCurrentRow = Range(SecondHalfCurrentRNG).Row
If FirstHalfBaseColNum = FirstHalfCurrentColNum And SecondHalfBaseColNum = SecondHalfCurrentColNum And SecondHalfBaseRow + 1 = FirstHalfCurrentRow Then ' RangeCollection(2) is adjacent and below RangeCollection(1)
MergedRNG = FirstHalfBaseLetter & FirstHalfBaseRow & ":" & SecondHalfCurrentLetter & SecondHalfCurrentRow
BaseRNGisMerged = True
MergeCount = MergeCount + 1
LoopCount = 0
For x = 1 To RangeCollection.Count - MergeCount
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
ElseIf FirstHalfBaseColNum = FirstHalfCurrentColNum And SecondHalfBaseColNum = SecondHalfCurrentColNum And FirstHalfBaseRow - 1 = SecondHalfCurrentRow Then ' RangeCollection(2) is adjacent and above RangeCollection(1)
MergedRNG = FirstHalfCurrentLetter & FirstHalfCurrentRow & ":" & SecondHalfBaseLetter & SecondHalfBaseRow
BaseRNGisMerged = True
MergeCount = MergeCount + 1
LoopCount = 0
For x = 1 To RangeCollection.Count - MergeCount
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
End If
ElseIf BaseRNGisMerged = False And LoopRNGisMerged = False Then
BaseLetter = Split(Columns(Range(RNGtoCompare).Column).Address(, False), ":")(1)
BaseColNum = Range(BaseLetter & 1).Column
BaseRow = Range(RNGtoCompare).Row
CurrentLetter = Split(Columns(Range(CurrentRNGValue).Column).Address(, False), ":")(1)
CurrentColNum = Range(CurrentLetter & 1).Column
CurrentRow = Range(CurrentRNGValue).Row
If BaseLetter = CurrentLetter And BaseRow + 1 = CurrentRow Then
MergedRNG = BaseLetter & BaseRow & ":" & BaseLetter & CurrentRow
BaseRNGisMerged = True
LoopCount = 0
For x = 1 To RangeCollection.Count - MergeCount
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
ElseIf BaseLetter = CurrentLetter And BaseRow - 1 = CurrentRow Then
MergedRNG = BaseLetter & CurrentRow & ":" & BaseLetter & BaseRow
BaseRNGisMerged = True
LoopCount = 0
For x = 1 To RangeCollection.Count - MergeCount
If RangeCollection(x) = CurrentRNGValue Or RangeCollection(x) = RNGtoCompare Then
CurrX = RangeCollection(x)
If RangeCollection(x) = CurrentRNGValue Then
LoopCount = LoopCount + 1
ElseIf RangeCollection(x) = RNGtoCompare Then
LoopCount = LoopCount + 1
End If
RangeCollection.Remove (x)
x = x - 1
If LoopCount = 2 Then
Exit For
End If
End If
Next x
RangeCollection.Add MergedRNG
End If
End If
End If
RangeCollectionCount = RangeCollection.Count
RNGtoCompare = ""
CurrentRNGValue = ""
MergedRNG = ""
' END of VERTICAL GROUPING
If RangeCollection.Count > 0 Then
For n = 1 To RangeCollection.Count
If Not Right(CombinedRanges, 1) = "," And Len(CombinedRanges) > 0 Then
CombinedRanges = CombinedRanges & ","
End If
CombinedRanges = CombinedRanges & RangeCollection(n) & ","
Next n
CombinedRanges = Left(CombinedRanges, Len(CombinedRanges) - 1)
GroupCollectionRNG = CombinedRanges
End If
Set RangeCollection = Nothing
Application.ScreenUpdating = True
End Function