VB Excel问题代码需要修改,求指正
现在的代码缺少排名过程,正确的程序代码如下图:
执行效果:
代码文本:
Option Explicit
Sub 排名()
Dim arr, rlt, dic, i, j, t, t2
arr = Range("a1").CurrentRegion
Set dic = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(arr)
dic(arr(i, 2)) = 0
Next i
ReDim rlt(1 To dic.Count, 1 To UBound(arr))
For Each t In dic.Keys
For Each t2 In dic.Keys
If t2 >= t Then dic(t) = dic(t) + 1
Next t2
'此时dic(t)为成绩t的名次
j = 2 '输出数组的列
For i = 2 To UBound(arr)
If arr(i, 2) = t Then
rlt(dic(t), j) = arr(i, 1)
j = j + 1
End If
Next i
Next t
For i = 1 To UBound(rlt)
rlt(i, 1) = "第" & i & "名"
Next i
Range("d1").Resize(UBound(rlt), UBound(rlt, 2)) = rlt '输出结果
End Sub