Sub test_01()
Dim Arr, xD, i&, T$, U&, TM
TM = Timer
Set xD = CreateObject("Scripting.Dictionary")
Arr = Range([C2], Cells(Rows.Count, 3).End(3))
For i = 1 To UBound(Arr)
T = Arr(i, 1): U = xD(T): Arr(i, 1) = ""
If U > 0 Then Arr(U, 1) = "重覆": xD(T) = -1: U = -1
If U < 0 Then Arr(i, 1) = "重覆"
If U = 0 Then xD(T) = i
Next i
[B2].Resize(UBound(Arr)) = Arr
MsgBox Timer - TM
End Sub
Sub test_02()
Dim Arr, Brr, xD, i&, TM
TM = Timer
Set xD = CreateObject("Scripting.Dictionary")
Arr = Range([C2], Cells(Rows.Count, 3).End(3))
ReDim Brr(1 To UBound(Arr), 0)
For i = 1 To UBound(Arr)
xD(Arr(i, 1) & "") = xD(Arr(i, 1) & "") + 1
Next i
For i = 1 To UBound(Arr)
If xD(Arr(i, 1) & "") > 1 Then Brr(i, 0) = "重覆"
Next
[B2].Resize(UBound(Arr)) = Brr
MsgBox Timer - TM
End Sub
MsgBox Application.Text(Time - xTime, ["計時 ss 秒"])
End Sub
複製代碼
作者: 千暉尋 時間: 2018-9-27 15:07
回復 准提部林
準大太厲害啦,原來字典物件輸入的key是字串型態的話,速度可以提昇那麼多!!!
如果 ...
n7822123 發表於 2018-8-31 01:11
3個語法,可看懂2個,唯獨准大的第1個程式碼,我想了好久,針對以下語法
For i = 1 To UBound(Arr)
T = Arr(i, 1): U = xD(T): Arr(i, 1) = ""
If U > 0 Then Arr(U, 1) = "重覆": xD(T) = -1: U = -1
If U < 0 Then Arr(i, 1) = "重覆"
If U = 0 Then xD(T) = i
Next i
還是想不透為何如此就能判斷出重覆與否,那位大大可以幫忙解說一下嗎?作者: jackyq 時間: 2018-9-28 08:37
Sub test_01a()
Dim Arr, xD, i&, T$, U&, TM
TM = Timer
Set xD = CreateObject("Scripting.Dictionary")
Arr = Range([C2], Cells(Rows.Count, 3).End(3))
For i = 1 To UBound(Arr)
T = Arr(i, 1): U = xD(T): Arr(i, 1) = ""
If U > 0 Then Arr(i, 1) = "重覆" ': xD(T) = -1: U = -1 'If U < 0 Then Arr(i, 1) = "重覆"
If U = 0 Then xD(T) = i
Next i
[B2].Resize(UBound(Arr)) = Arr
'MsgBox Timer - TM
End Sub作者: 准提部林 時間: 2018-9-28 10:01
Sub test_03()
Dim Arr, xD, i&, T$, U&, TM
TM = Timer
Set xD = CreateObject("Scripting.Dictionary")
Arr = Range([C2], Cells(Rows.Count, 3).End(3))
For i = UBound(Arr) To 1 Step -1
T = Arr(i, 1): U = xD(T): Arr(i, 1) = ""
If U > 0 Then Arr(U, 1) = "Rept": xD(T) = -1
If U = 0 Then xD(T) = i
Next i
[B2].Resize(UBound(Arr)) = Arr
MsgBox Timer - TM
End Sub作者: Qin 時間: 2018-10-7 14:24
Sub test_04()
Dim Arr, xD, i&, T$, U&, TM
TM = Timer
Set xD = CreateObject("Scripting.Dictionary")
Arr = Range([C2], Cells(Rows.Count, 3).End(3))
For i = UBound(Arr) To 1 Step -1
T = Arr(i, 1): U = xD(T): Arr(i, 1) = ""
If U > 0 Then Arr(U, 1) = "Rept": xD(T) = -1: U = -0
If U = 0 Then xD(T) = i
Next i
[B2].Resize(UBound(Arr)) = Arr
MsgBox Timer - TM
End Sub作者: Andy2483 時間: 2023-5-16 14:46
Option Explicit
Sub test_01()
Dim Arr, xD, i&, T$, U&, TM
'↑宣告變數
TM = Timer
Set xD = CreateObject("Scripting.Dictionary")
'↑令xD變數是 字典
Arr = Range([C2], Cells(Rows.Count, 3).End(3))
'↑令Arr變數是 二維陣列,以儲存格值帶入陣列中
For i = 1 To UBound(Arr)
'↑設順迴圈
T = Arr(i, 1): U = xD(T): Arr(i, 1) = ""
'↑令T變數是 Arr陣列值(關鍵字),
'令U變數是 關鍵字查xD字典的item值
'令清除掉Arr陣列裡的值
If U > 0 Then Arr(U, 1) = "重覆": xD(T) = -1: U = -1
'↑如果U變數大於0,代表此關鍵字在Arr陣列i列值還不是 "重覆",
'也就是初次檢查到重覆!就令Arr陣列U變數列值是 "重覆"字串,
'令此關鍵字的item改為-1,令U變數是 -1
If U < 0 Then Arr(i, 1) = "重覆"
'↑如果U變數小於0,代表Arr陣列這迴圈列值是重複的,
'就令Arr陣列i迴圈列值是 "重覆"字串
If U = 0 Then xD(T) = i
'↑如果U變數是 0,代表此關鍵字是第1次出現,
'只以此關鍵字當key,item是i迴圈數要納入xD字典裡就好
Next i
[B2].Resize(UBound(Arr)) = Arr
'↑令Arr陣列值從[B2]擴展的儲存格中寫入
MsgBox Timer - TM
End Sub