速度与稳定性之间必然要有所取舍。
Scripting.Dictionary经无数人使用应该是可靠的,如果速度相差不大还是用比较成熟的东西好。自己做的东东有时很难避免百密而有一疏,使用时的复杂性是我们很难都一一想到的。
Scripting.Dictionary是一个很重要的东东,不完全熟悉他是不可想象的! Sub CommandButton2_Click() 'Dictionary方法
Dim Sd As Scripting.Dictionary
Dim p&, q&, i&
Dim arr As Variant
Dim Xarr()
Dim temp
aa = Timer
Application.ScreenUpdating = False
Set Sd = New Scripting.Dictionary q = Sheet2.[a65536].End(xlUp).Row
arr = Sheet2.Range("a1:b" & q)
On Error Resume Next '防止原始数据里有重复的情况
For i = 1 To q
Sd.Add CStr(arr(i, 1)), arr(i, 2)
Next
Err.Clear
On Error GoTo 0 p = [a65536].End(xlUp).Row
arr1 = Range("a1:a" & p)
ReDim Xarr(1 To p, 0)
For i = 2 To 22686 '
If arr1(i, 1) <> "" Then
temp = ""
temp = Sd(CStr(arr1(i, 1)))
If temp <> "" Then
Xarr(i, 0) = temp
Else
Xarr(i, 0) = "n"
End If
Else
Xarr(i, 0) = "K"
End If
Next
Set Sd = Nothing
[c1].Resize(p, 1) = Xarr
Application.ScreenUpdating = True
MsgBox Format(Timer - aa, "0.000")
End Sub |