Sub AwTest3()
Dim i&, j%, k%, n%, l%, m&, x&, r&, Ra&, Rb&, kSr
Dim Arr, Brr, Crr, Iar, Tar, Tbr(), d As Object, dd As Object
Set d = CreateObject("Scripting.Dictionary")
Set dd = CreateObject("Scripting.Dictionary")
Arr = Range("A1").CurrentRegion
For i = 2 To UBound(Arr)
If Not d.exists(Arr(i, 1)) Then Set d(Arr(i, 1)) = CreateObject("Scripting.Dictionary")
ReDim Tar(1 To 2): Tar(1) = i
d(Arr(i, 1))(Arr(i, 2)) = Tar
Next
Brr = Range("E1").CurrentRegion
For i = 2 To UBound(Brr)
If Not d.exists(Brr(i, 1)) Then
Set d(Brr(i, 1)) = CreateObject("Scripting.Dictionary")
ReDim Tar(1 To 2): Tar(2) = i
d(Brr(i, 1))(Brr(i, 2)) = Tar
Else
If Not d(Brr(i, 1)).exists(Brr(i, 2)) Then
ReDim Tar(1 To 2): Tar(2) = i
d(Brr(i, 1))(Brr(i, 2)) = Tar
Else
Tar = d(Brr(i, 1))(Brr(i, 2))
Tar(2) = i
d(Brr(i, 1))(Brr(i, 2)) = Tar
End If
End If
Next
ReDim Crr(1 To UBound(Arr) + UBound(Brr), 1 To 7)
For Each kSr In d
Iar = d(kSr).Items
n = 0: l = 0: Erase Tar: Erase Tbr
For k = 0 To UBound(Iar)
Tar = Iar(k)
Ra = Val(Tar(1)): Rb = Val(Tar(2))
If Ra Then
r = r + 1
For j = 1 To UBound(Arr, 2)
Crr(r, j) = Arr(Ra, j)
Next
If Rb Then
For j = 1 To UBound(Brr, 2)
Crr(r, j + 4) = Brr(Rb, j)
Next
Else
n = n + 1
ReDim Preserve Tbr(1 To n): Tbr(n) = r '记录右边可排列数据的空白位置行号
dd(Arr(Ra, 2)) = r
End If
Else
If n Then
l = l + 1
For j = 1 To UBound(Brr, 2)
Crr(Tbr(l), j + 4) = Brr(Rb, j)
Next
n = n - 1
If dd.exists(Arr(Tbr(l), 2)) Then dd.Remove (Arr(Tbr(l), 2)) '这句修改一下
Else
m = dd(Brr(Rb, 2))
If m Then x = m Else r = r + 1: x = r
For j = 1 To UBound(Brr, 2)
Crr(x, j + 4) = Brr(Rb, j)
Next
End If
End If
Next
Next
If r Then Range("Y2").Resize(r, 7) = Crr
End Sub
|