Updated code:
Sub tgr()
Dim rngCheck As Range
Dim rngCell As Range
Dim rngArea As Range
Dim rngGroups(1 To 65000) As Range
Dim arrGroups(1 To 65000) As Long
Dim dAvg(1 To 65000) As Double
Dim arrLoc(1 To 65000) As String
Dim lCount As Long
Dim lMax As Long
Dim lCommon As Long
Dim CommonIndex As Long
Dim i As Long
Set rngCheck = Intersect(ActiveSheet.UsedRange, Columns("C"))
Set rngCheck = rngCheck.Resize(rngCheck.Rows.Count + 1)
For Each rngCell In rngCheck.Cells
If rngCell.Value < 0 Then
lCount = lCount + 1
Else
If lCount > 0 Then
If lCount > lMax Then lMax = lCount
arrGroups(lCount) = arrGroups(lCount) + 1
If arrGroups(lCount) > lCommon Then
lCommon = arrGroups(lCount)
CommonIndex = lCount
End If
Select Case (rngGroups(lCount) Is Nothing)
Case True: Set rngGroups(lCount) = rngCell.Offset(-lCount).Resize(lCount)
Case Else: Set rngGroups(lCount) = Union(rngGroups(lCount), rngCell.Offset(-lCount).Resize(lCount))
End Select
arrLoc(lCount) = arrLoc(lCount) & "," & rngCell.Offset(-lCount).Resize(lCount).Address(0, 0)
End If
lCount = 0
End If
Next rngCell
If lMax > 0 Then
For i = 1 To lMax
If Not rngGroups(i) Is Nothing Then
For Each rngArea In rngGroups(i).Areas
dAvg(i) = dAvg(i) + rngArea.Offset(-1).Cells(1).Value
Next rngArea
dAvg(i) = dAvg(i) / rngGroups(i).Areas.Count
arrLoc(i) = Mid(arrLoc(i), 2)
Else
dAvg(i) = 0
End If
Next i
Range("J26:M" & Rows.Count).ClearContents
Range("J26").Resize(lMax).Value = Application.Transpose(Application.Transpose(Evaluate("Index(Row(1:" & lMax & "),)")))
Range("K26").Resize(lMax).Value = Application.Transpose(arrGroups)
Range("L26").Resize(lMax).Value = Application.Transpose(dAvg)
Range("M26").Resize(lMax).Value = Application.Transpose(arrLoc)
With Range("J25:M25")
.Value = Array("Group Size", "Appearances", "Positive Average", "Location")
.Font.Bold = True
.Borders(xlEdgeBottom).LineStyle = xlContinuous
'.Resize(, 3).EntireColumn.AutoFit
End With
Range("J25").Resize(lMax + 1, 4).Sort Range("K25"), xlDescending, Header:=xlYes
Range("J25:J" & Rows.Count).Find(lMax).Resize(, 4).Interior.ColorIndex = 6
End If
Set rngCheck = Nothing
Set rngCell = Nothing
Set rngArea = Nothing
Erase rngGroups
Erase arrGroups
Erase dAvg
Erase arrLoc
End Sub
Bookmarks