|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
如附件,两个表的B列数据比较,若表1没有独有数据或者表2没有独有数据,就会报错。
请老师帮忙优化代码,谢谢
Sub 位号差异()
Dim arr, brr, d As Object, dc As Object, i%, j%
arr = Sheets("表1").[B1].CurrentRegion
brr = Sheets("表2").[B1].CurrentRegion
Set d = CreateObject("scripting.dictionary")
Set dc = CreateObject("scripting.dictionary")
'----------------------------------------------
With Sheets("结果")
Range("2:" & Rows.Count).Clear '清除第一行除外的所有行
End With
'----------------------------------------------
For i = 2 To UBound(arr)
d(arr(i, 2)) = ""
Next i
For j = 2 To UBound(brr)
If d.exists(brr(j, 2)) Then
d.Remove (brr(j, 2))
End If
Next j
'----------------------------------------------
For i = 2 To UBound(brr)
dc(brr(i, 2)) = ""
Next i
For j = 2 To UBound(arr)
If dc.exists(arr(j, 2)) Then
dc.Remove (arr(j, 2))
End If
Next j
Sheets("结果").[B2].Resize(d.Count, 1) = Application.Transpose(d.keys)
Sheets("结果").[A2].Resize(dc.Count, 1) = Application.Transpose(dc.keys)
Set d = Nothing
Set dc = Nothing
End Sub
|
|