Sub Macro1()
Dim lrs As Long
Dim i As long
Dim lro As Long
Dim lrh As Long
lrs = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lro = Sheets("Sheet1").Cells(Rows.Count, 14).End(xlUp).Row + 1
lrh = Sheets("Sheet1").Cells(Rows.Count, 27).End(xlUp).Row + 1
Application.ScreenUpdating = False
Range("N2:Q" & lro).Clear
Range("AA2:AB" & lrh).Clear
Range("A2:A" & lrs).Copy Range("AA2")
Application.CutCopyMode = False
lrh = Sheets("Sheet1").Cells(Rows.Count, 27).End(xlUp).Row
ActiveSheet.Range("$AA$1:$AA$" & lrh).RemoveDuplicates Columns:=1, Header:= _
xlYes
lrh = Sheets("Sheet1").Cells(Rows.Count, 27).End(xlUp).Row
For i = 2 To lrh
Range("AB" & i) = Application.WorksheetFunction.CountIf(Range("A2:A" & lrs), Range("AA" & i))
Next i
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("AB2:AB" & lrh _
), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("AA2:AB" & lrh)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
lrh = Sheets("Sheet1").Cells(Rows.Count, 27).End(xlUp).Row
ko = 2
For i = 2 To lrh
For j = 2 To lrs
If Range("A" & j).Value = Range("AA" & i) Then
Range(Cells(j, 1), Cells(j, 4)).Copy Range("N" & ko)
ko = ko + 1
End If
Next j
Next i
End Sub