|
Sub AwTest2()
Dim i&, j%, k%, m%, n%, l%, r&, Ra&, Rb&, kSr, Arr, Brr, Crr, Iar, Tar(), d As Object
Set d = 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")
d(Arr(i, 1))(Arr(i, 2)) = i & "|"
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")
d(Brr(i, 1))(Brr(i, 2)) = "|" & i
Else
If Not d(Brr(i, 1)).Exists(Brr(i, 2)) Then
d(Brr(i, 1))(Brr(i, 2)) = "|" & i
Else
d(Brr(i, 1))(Brr(i, 2)) = d(Brr(i, 1))(Brr(i, 2)) & i
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
For k = 0 To UBound(Iar)
m = InStr(Iar(k), "|")
Ra = 0: Rb = 0
' l = 0 l 位置修改一下
If m = Len(Iar(k)) Then
Ra = Split(Iar(k), "|")(0)
ElseIf m = 1 Then
Rb = Split(Iar(k), "|")(1)
Else
Ra = Split(Iar(k), "|")(0)
Rb = Split(Iar(k), "|")(1)
End If
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 Tar(1 To n): Tar(n) = r '记录右边可排列数据的空白位置行号
End If
Else
If n Then
l = l + 1
For j = 1 To UBound(Brr, 2)
Crr(Tar(l), j + 4) = Brr(Rb, j)
Next
n = n - 1
Else
r = r + 1
For j = 1 To UBound(Brr, 2)
Crr(r, j + 4) = Brr(Rb, j)
Next
End If
End If
Next
Next
If r Then Range("Y2").Resize(r, 7) = Crr
End Sub |
|