
Option Explicit
Sub abc()
Dim a, i, j, n
a = [a1].CurrentRegion.Value
ReDim b(1 To UBound(a), 1 To (UBound(a, 2) - 1) * 3)
For j = 2 To UBound(a, 2)
Call bsort(a, 2, UBound(a), 1, UBound(a, 2), j)
n = n + 3: b(1, n) = "排名"
For i = 1 To UBound(a)
b(i, n - 2) = a(i, 1): b(i, n - 1) = a(i, j)
Next
Call rank(b, 2, UBound(b), n - 1, n, True)
Next
[a1].Offset(, UBound(a, 2) + 1).Resize(UBound(b), UBound(b, 2)) = b
End Sub
Function bsort(a, first, last, left, right, key)
Dim i, j, k, t
For i = first To last - 1
For j = first To last + first - 1 - i
If a(j, key) < a(j + 1, key) Then
For k = left To right
t = a(j, k): a(j, k) = a(j + 1, k): a(j + 1, k) = t
Next
End If
Next
Next
End Function
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
菠萝蜜