上代码、上附件:- Sub ArrTest()
- arr = WorksheetFunction.Transpose(Selection)
- srr = Rnk(arr)
-
- Stop
- End Sub
- Function Rnk(Rng_Arr, Optional n_OutputIndex& = 0, Optional p_OutputMode& = 2, Optional z_SortMode& = 0)
- Dim i&, t, u&
-
- On Error GoTo IsArr
- u = Rng_Arr.Count
- GoTo GetU
- IsArr:
- u = UBound(Rng_Arr)
- GetU:
- ReDim ar(1 To u, 1 To 2)
- For i = 1 To u
- ar(i, 1) = Rng_Arr(i)
- ar(i, 2) = i
- Next
- Call ShellSortArr(ar, 1, 2, u, z_SortMode) 'Sort by Score or TextValue (Column 1)
-
- If p_OutputMode = 0 Then 'Nature Sequence Rank
- For i = 1 To u
- ar(i, 1) = i
- Next
- ElseIf p_OutputMode = 1 Then 'West Rank
- t = ar(1, 1): ar(1, 1) = 1
- For i = 2 To u
- If ar(i, 1) = t Then ar(i, 1) = ar(i - 1, 1) Else t = ar(i, 1): ar(i, 1) = i
- Next
- ElseIf p_OutputMode = 2 Then 'Chinese Rank
- t = ar(1, 1): k = 1: ar(1, 1) = 1
- For i = 2 To u
- If ar(i, 1) <> t Then t = ar(i, 1): k = k + 1
- ar(i, 1) = k
- Next
- End If
-
- Call ShellSortArr(ar, 2, 1, u, 1) 'Sort back by Sequence No (Column 2)
-
- If n_OutputIndex = 0 Then Rnk = Application.Index(ar, , 1) Else Rnk = ar((n_OutputIndex - 1) Mod u + 1, 1)
-
- End Function
- Sub ShellSortArr(tr, x&, y&, u&, Optional z_SortMode& = 0) 'QuickShellSort
- Dim h&, i&, j&, k&, t1, t2
- h = u
- Do
- h = (h \ 5) * 2 + 1
- For i = h + 1 To u
- t1 = tr(i, x): t2 = tr(i, y)
- For j = i - h To 1 Step -h
- If z_SortMode Then
- If tr(j, x) < t1 Then Exit For 'z_SortMode=1 A-Z Sort
- Else
- If tr(j, x) > t1 Then Exit For 'z_SortMode=0 Z-A Sort
- End If
- tr(j + h, x) = tr(j, x): tr(j + h, y) = tr(j, y)
- Next
- tr(j + h, x) = t1: tr(j + h, y) = t2
- Next
- Loop Until h = 1
- End Sub
复制代码 |