Sub Macro3() Dim txt As String
Dim bdarr As Variant
Worksheets("Sheet1").Range("B:B").Copy Destination:=ActiveSheet.Range("A:A")
With Worksheets("Sheet1")
lastrow = .Cells(Rows.Count, "B").End(xlUp).Row
End With
' Selection.Copy
Sheets("Sheet2").Select
d1 = Cells(1, 4).Value
e1 = Cells(1, 5).Value
ActiveSheet.Range("$A$1:$A$82452").RemoveDuplicates Columns:=1, Header:= _
xlNo
outarr = Range(Cells(1, 2), Cells(7881, 6))
cola = Range(Cells(1, 1), Cells(7881, 1))
With Worksheets("Sheet1")
For i = 2 To 7881
For k = 1 To 4
outarr(i, k) = ""
Next k
txt = cola(i, 1)
If txt <> "" Then
outarr(i, 1) = Application.WorksheetFunction.VLookup(txt, Range(.Cells(1, 2), .Cells(lastrow, 4)), 3, False)
outarr(i, 2) = Left(outarr(i, 1), 1)
outarr(i, 3) = Application.WorksheetFunction.CountIfs(Range(.Cells(1, 2), .Cells(lastrow, 2)), txt, Range(.Cells(1, 5), .Cells(lastrow, 5)), d1)
outarr(i, 4) = Application.WorksheetFunction.CountIfs(Range(.Cells(1, 2), .Cells(lastrow, 2)), txt, Range(.Cells(1, 5), .Cells(lastrow, 5)), e1)
outarr(i, 5) = outarr(i, 3) + outarr(i, 4)
End If
Next i
End With
Range(Cells(1, 2), Cells(7881, 6)) = outarr
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("F2:F7881" _
), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet2").Sort
.SetRange Range("A1:F7881")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
End Sub