|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
老师,这段代码似乎运行的速度更加快一点。在您面前献丑了!
Sub test()
Dim t
Dim dic1 As Object, dic2 As Object
Dim arr(), brr()
'crr,drr,frr三个数组存放结果,提前设置好维度,在写入单元格时可以省略调用工作表transpose函数,如果数据量大,尽可能将数组的维度定义的大一点
Dim crr(1 To 10, 1 To 1), drr(1 To 10, 1 To 1), frr(1 To 10, 1 To 1)
Dim m%, n%, i%, j%, x%, y%, k%
Dim A_str$, B_str$
t = Timer
Set dic1 = CreateObject("scripting.dictionary")
Set dic2 = CreateObject("scripting.dictionary")
arr = Range("a1", Cells(Rows.Count, 1).End(3))
brr = Range("b1", Cells(Rows.Count, 2).End(3))
'将A列非空单元格写入字典
For m = 1 To UBound(arr)
If arr(m, 1) <> "" Then
dic1(arr(m, 1)) = ""
End If
Next
'将B列非空单元格写入字典
For n = 1 To UBound(brr)
If brr(n, 1) <> "" Then
dic2(brr(n, 1)) = ""
End If
Next
'crr数组为A列有B列没有,frr数组是两列同时有
For i = 0 To dic1.Count - 1
A_str = dic1.keys()(i)
If Not dic2.exists(A_str) Then
j = j + 1
crr(j, 1) = A_str
End If
If dic2.exists(A_str) Then
k = k + 1
frr(k, 1) = A_str
End If
Next
'B列有A列没有
For x = 0 To dic2.Count - 1
B_str = dic2.keys()(x)
If Not dic1.exists(B_str) Then
y = y + 1
drr(y, 1) = B_str
End If
Next
Sheet2.Range("e2").Resize(j, 1) = crr
Sheet2.Range("f2").Resize(y, 1) = drr
Sheet2.Range("g2").Resize(k, 1) = frr
Erase arr: Erase brr: Erase crr: Erase drr: Erase frr
dic1.RemoveAll: dic2.RemoveAll
Set dic1 = Nothing
Set dic2 = Nothing
MsgBox Timer - t
End Sub
|
|