|
发表于 2013-5-8 22:24
|
显示全部楼层
本楼为最佳答案
对于实际应用中如果列数不一致,做下判断。- Sub test()
- '---------------------------------------------------------------------------------------
- ' Procedure : test
- ' Author : hwc2ycy
- ' Date : 2013/5/8
- ' Purpose : 把每行数据转化为字符串作为KEY,以数组形式做为ITEM值存入字典,实现查找相同行
- '---------------------------------------------------------------------------------------
- '
- Dim arrA, arrB, arrTemp, i As Long, strTemp$
- Dim key1
- arrA = Range("a2:f6")
- arrB = Range("h2:m5")
-
- If UBound(arrA, 2) <> UBound(arrB, 2) Then MsgBox "数据列数不一致", vbCritical: Exit Sub
-
- Dim objDicA As Object, objDicB As Object
- Set objDicA = CreateObject("scripting.dictionary")
- Set objDicB = CreateObject("scripting.dictionary")
- For i = LBound(arrA) To UBound(arrA)
- arrTemp = WorksheetFunction.Index(arrA, i, 0)
- strTemp = Join(arrTemp, "#")
- objDicA(strTemp) = arrTemp
- Next
- For i = LBound(arrB) To UBound(arrB)
- arrTemp = WorksheetFunction.Index(arrB, i, 0)
- strTemp = Join(arrTemp, "#")
- objDicB(strTemp) = arrTemp
- Next
- i = Cells(Rows.Count, "o").End(xlUp).Row
- If i > 1 Then Range("o2:t" & i).ClearContents
- Application.ScreenUpdating = False
- For Each key1 In objDicA.Keys
- If objDicB.exists(key1) Then
- Cells(Rows.Count, "o").End(xlUp).Offset(1).Resize(, UBound(arrA, 2)) = objDicA(key1)
- End If
- Next
- Set objDicA = Nothing
- Set objDicB = Nothing
- Application.ScreenUpdating = True
- MsgBox "查找完成"
- End Sub
- Sub test2()
- '---------------------------------------------------------------------------------------
- ' Procedure : test2
- ' Author : hwc2ycy
- ' Date : 2013/5/8
- ' Purpose :把每行数据转化为字符串作为KEY,把数组所在行做为ITEM值存入字典,实现查找相同行
- '---------------------------------------------------------------------------------------
- '
- Dim arrA, arrB, arrTemp, i As Long, strTemp$
- Dim key1
-
- arrA = Range("a2:f6")
- arrB = Range("h2:m5")
- If UBound(arrA, 2) <> UBound(arrB, 2) Then MsgBox "数据列数不一致", vbCritical: Exit Sub
-
- Dim objDicA As Object, objDicB As Object
- Set objDicA = CreateObject("scripting.dictionary")
- Set objDicB = CreateObject("scripting.dictionary")
- For i = LBound(arrA) To UBound(arrA)
- arrTemp = WorksheetFunction.Index(arrA, i, 0)
- strTemp = Join(arrTemp, "#")
- objDicA(strTemp) = i
- Next
- For i = LBound(arrB) To UBound(arrB)
- arrTemp = WorksheetFunction.Index(arrB, i, 0)
- strTemp = Join(arrTemp, "#")
- objDicB(strTemp) = i
- Next
- i = Cells(Rows.Count, "o").End(xlUp).Row
- If i > 1 Then Range("o2:t" & i).ClearContents
- Application.ScreenUpdating = False
- For Each key1 In objDicA.Keys
- If objDicB.exists(key1) Then
- Cells(Rows.Count, "o").End(xlUp).Offset(1).Resize(, UBound(arrA, 2)) = WorksheetFunction.Index(arrA, objDicA(key1), 0)
- End If
- Next
- Set objDicA = Nothing
- Set objDicB = Nothing
- Application.ScreenUpdating = True
- MsgBox "查找完成"
- End Sub
复制代码 |
|