請測試看看,謝謝
Sub test()
Dim Arr, Brr, xD, T
Set xD = CreateObject("Scripting.Dictionary")
With Range("h6:h" & Cells(Rows.Count, 8).End(xlUp).Row)
Brr = .Value
.Sort key1:=.Item(1), Order1:=2, Header:=2
Arr = .Value
.Value = Brr
End With
For i = 1 To UBound(Arr)
T = Arr(i, 1): If T = "" Then GoTo 98
If xD(T) = "" Then: n = n + 1: xD(T) = n
98: Next
For i = 1 To UBound(Brr)
T = Brr(i, 1)
If T = "" Then Brr(i, 1) = 0: GoTo 99
Brr(i, 1) = xD(T)
99: Next
Range("i6").Resize(UBound(Brr)) = Brr
End Sub作者: samwang 時間: 2021-9-28 14:09
Sub test2()
Dim Arr, xD, a, T, i&
Set xD = CreateObject("Scripting.Dictionary")
Tm = Timer
Arr = Range("h6:h" & Cells(Rows.Count, 8).End(xlUp).Row)
For i = 1 To UBound(Arr)
T = Arr(i, 1): If T <> "" Then xD(T) = ""
Next
For i = 1 To xD.Count
a = Application.Large(xD.keys, i)
n = n + 1: xD(a) = n
Next
For i = 1 To UBound(Arr)
T = Arr(i, 1)
If T = "" Then Arr(i, 1) = 0: GoTo 99
Arr(i, 1) = xD(T)
99: Next
Range("j6").Resize(UBound(Arr)) = Arr
MsgBox Timer - Tm
End Sub作者: 准提部林 時間: 2021-10-2 10:38
Sub 排名()
Dim Arr, Brr, xD, V&, i&, j&, k&, N&
Set xD = CreateObject("Scripting.Dictionary")
Arr = Range([h1], [h1].Cells(Rows.Count, 1).End(3))
ReDim Brr(1 To UBound(Arr) + 1)
For i = 6 To UBound(Arr)
If Arr(i, 1) = "" Then GoTo i01
V = Arr(i, 1)
If xD.Exists(V) Then GoTo i01 Else xD(V) = "": N = N + 1
For j = N To 1 Step -1
If V >= Brr(j) Then Brr(j + 1) = Brr(j) Else Exit For
Next j
Brr(j + 1) = V
i01: Next i
xD.removeall
For j = 1 To N: xD(Brr(j)) = j: Next
'------------------------------------
For i = 6 To UBound(Arr)
V = xD(Arr(i, 1))
If Arr(i, 1) = "" Then V = 0
Arr(i - 5, 1) = V
Next i
[i6].Resize(UBound(Arr) - 5) = Arr
Set xD = Nothing: Erase Arr, Brr
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
With Target
If .Column <> [h1].Column Then Exit Sub
If .Row < 6 Or .Count > 1 Then Exit Sub
Call 排名test02
End With
End Sub
'參考 ikboy 學習System.collections.arraylist
Sub 排名AmoKat修改()
tm = Timer
Dim d As Object, a, k, t, xL As Object, n&
a = Range("h6:h" & [h1048576].End(3).Row)
Set d = CreateObject("scripting.dictionary") '字典取唯一值
For Each k In a: If k <> "" Then d(k) = 0: Next
Set xL = CreateObject("System.collections.arraylist") '排序作業
For Each k In d.keys: xL.Add k: Next
xL.Sort 'Sorted Ascending
xL.Reverse 'Reverse sort
For i = 1 To xL.Count: d(xL(i - 1)) = i: Next '排序序號(排名)寫入字典
For i = 1 To UBound(a)
If a(i, 1) = "" Then a(i, 1) = 0 Else a(i, 1) = d(a(i, 1)) '排名寫入a陣列
Next
[i6].Resize(UBound(a)) = a 'a陣列寫入 I 欄位
Debug.Print Timer - tm, "End" '31.40" 改善為 0.332"
End Sub作者: ML089 時間: 2021-10-19 16:42
Sub 排名AmoKat()
tm = Timer
Set xD = CreateObject("Scripting.Dictionary")
Arr = Range([h6], [h1].Cells(Rows.Count, 1).End(xlUp))
For i = 1 To UBound(Arr): xD(Arr(i, 1)) = 0: Next i '字典產生唯一值
With [i6].Resize(xD.Count) '利用 Range.Sort 排序
.Value = Application.Transpose(xD.keys)
.Sort Key1:=.Item(1), Order1:=xlDescending, Header:=xlNo
End With
For i = 1 To xD.Count: xD(Cells(5 + i, "I").Value) = i: Next i '排序序號寫入字典
For i = 1 To UBound(Arr) '查詢字典排列序號
If Arr(i, 1) = "" Then Arr(i, 1) = 0 Else Arr(i, 1) = xD(Arr(i, 1))
Next i
[i6].Resize(UBound(Arr)) = Arr '貼上資料
Set xD = Nothing: Erase Arr
Debug.Print Timer - tm '0.30"
End Sub