本帖最后由 一把小刀闯天下 于 2018-7-21 11:20 编辑
'20楼附件,猜一个
Option Explicit
Sub test()
Dim arr, dic, i, j, m, n, pos
Set dic = CreateObject("scripting.dictionary")
arr = Sheets("1").[a1].CurrentRegion
For i = 2 To UBound(arr, 1)
If Not dic.exists(arr(i, 4)) Then n = n + 1: dic(arr(i, 4)) = n
Next
arr = Sheets("2").[a1].CurrentRegion
ReDim brr(1 To UBound(arr, 1), 1 To UBound(arr, 2)): n = 0
For i = 2 To UBound(arr, 1)
If dic.exists(arr(i, 4)) Then
n = n + 1: brr(n, 1) = n
For j = 2 To UBound(arr, 2): brr(dic(arr(i, 4)), j) = arr(i, j): Next
Else
MsgBox "!!": Exit Sub
End If
Next
ReDim mark(1 To UBound(arr, 2))
For i = 1 To UBound(mark): mark(i) = arr(1, i): Next
i = n \ 40 + IIf(n Mod 40 = 0, 0, 1)
ReDim arr(1 To 80, 1 To UBound(brr, 2) * i)
For i = 1 To n
m = m + 1
For j = 1 To UBound(brr, 2)
arr((m - 1) * 2 + 1, pos + j) = mark(j)
arr((m - 1) * 2 + 2, pos + j) = brr(i, j)
Next
If m Mod 40 = 0 Then m = 0: pos = pos + UBound(brr, 2)
Next
With Sheets("3").[a2]
.Resize(Rows.Count - 1, UBound(brr, 2)).ClearContents
If n > 0 Then .Resize(n, UBound(brr, 2)) = brr
End With
With Sheets("4").[a1]
.Resize(Rows.Count, UBound(arr, 2)).ClearContents
if n>0 then .Resize(UBound(arr, 1), UBound(arr, 2)) = arr
End With
End Sub |