Option Explicit
Sub Top3()
Dim wsFiltered As Worksheet, wsStats As Worksheet, wsTemp As Worksheet
Dim rFiltered As Range, rTemp As Range, rTemp1 As Range
Dim i As Long, n As Long
Application.ScreenUpdating = False
'init
Application.ScreenUpdating = False
Set wsFiltered = Worksheets("FilteredSet")
Set wsStats = Worksheets("BHAStats")
Set rFiltered = wsFiltered.Cells(1, 1).CurrentRegion
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Temp").Delete
Application.DisplayAlerts = True
On Error GoTo 0
Worksheets.Add.Name = "Temp"
Set wsTemp = ActiveSheet
'copy filtered data to temp
rFiltered.Columns(6).Copy wsTemp.Cells(1, 1)
rFiltered.Columns(7).Copy wsTemp.Cells(1, 2)
rFiltered.Columns(8).Copy wsTemp.Cells(1, 3)
rFiltered.Columns(9).Copy wsTemp.Cells(1, 4)
rFiltered.Columns(62).Copy wsTemp.Cells(1, 5)
Set rTemp = wsTemp.Cells(1, 1).CurrentRegion
Set rTemp1 = rTemp.Cells(2, 1).Resize(rTemp.Rows.Count - 1, rTemp.Columns.Count)
'sort temp
With wsTemp.Sort
.SortFields.Clear
.SortFields.Add Key:=rTemp1.Columns(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=rTemp1.Columns(2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=rTemp1.Columns(3), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=rTemp1.Columns(4), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=rTemp1.Columns(5), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SetRange rTemp
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'look for first (highest) 3 matches
With rTemp
For i = 2 To .Rows.Count - 1
If (.Cells(i, 1).Value = .Cells(i + 1, 1).Value) And _
(.Cells(i, 2).Value = .Cells(i + 1, 2).Value) And _
(.Cells(i, 3).Value = .Cells(i + 1, 3).Value) And _
(.Cells(i, 4).Value = .Cells(i + 1, 4).Value) Then
n = n + 1
If n > 2 Then .Cells(i + 1, 5).Value = True ' marker
Else
n = 0
End If
Next i
End With
'delete TRUE rows
On Error Resume Next
rTemp.Columns(5).SpecialCells(xlCellTypeConstants, xlLogical).EntireRow.Delete
On Error GoTo 0
'copy over to Stats (Note does not clear existing data)
wsTemp.Columns(1).Copy wsStats.Cells(1, 6)
wsTemp.Columns(2).Copy wsStats.Cells(1, 7)
wsTemp.Columns(3).Copy wsStats.Cells(1, 8)
wsTemp.Columns(4).Copy wsStats.Cells(1, 9)
wsTemp.Columns(5).Copy wsStats.Cells(1, 62)
'delete temp worksheet
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Temp").Delete
Application.DisplayAlerts = True
On Error GoTo 0
Application.ScreenUpdating = True
End Sub