'假设A、D列已经有序,否则先排下序
Option Explicit
Sub abc()
Dim a, i, p
a = [a1].CurrentRegion.Offset(1).Resize(, 5).Value
For i = 1 To UBound(a) - 1
If a(i, 1) <> a(i + 1, 1) Then
Call rank(a, p + 1, i, 4, 5, True) 'false为中式排名
p = i
End If
Next
[a2].Resize(UBound(a) - 1, UBound(a, 2)) = a
End Sub
Function rank(a, first, last, key, col, order As Boolean)
Dim i As Long, j As Long, m As Long
m = 1: a(first, col) = 1
For i = first + 1 To last
If order Then
m = m + 1
Else
If a(i, key) <> a(i - 1, key) Then m = m + 1
End If
If a(i, key) = a(i - 1, key) Then
a(i, col) = a(i - 1, col)
Else
a(i, col) = m
End If
Next
End Function