|
Sub 批量生成()
Application.ScreenUpdating = False
Dim ar As Variant
Dim d As Object
Dim arr()
Set d = CreateObject("scripting.dictionary")
ReDim arr(1 To Sheets.Count)
With Sheets("表一")
r = .Cells(Rows.Count, 1).End(xlUp).Row
If r < 2 Then MsgBox "表一为空!": End
ar = .Range("a1:h" & r)
End With
For i = 2 To UBound(ar)
If VBA.Trim(ar(i, 2)) <> "" Then
d(VBA.Trim(ar(i, 2))) = ar(i, 1)
End If
Next i
With Sheets("打印表")
rs = .Cells(Rows.Count, 1).End(xlUp).Row
If rs > 33 Then .Rows("34:" & rs).Delete
.Range("a4:f31") = Empty
For Each k In d.keys
n = 0: m = m + 1
rq = d(k)
ReDim arr(1 To UBound(ar), 1 To UBound(ar, 2) - 2)
For i = 2 To UBound(ar)
If VBA.Trim(ar(i, 2)) = k Then
n = n + 1
For j = 3 To UBound(ar, 2)
arr(n, j - 2) = ar(i, j)
Next j
End If
Next i
If m = 1 Then
.Cells(4, 1).Resize(n, UBound(arr, 2)) = arr
.[b2] = k
.[f2] = rq
Else
ws = .Cells(Rows.Count, 1).End(xlUp).Row + 1
.Rows("1:33").Copy .Cells(ws, 1)
.Cells(ws + 3, 1).Resize(n, UBound(arr, 2)) = arr
.Cells(ws + 1, 2) = k
.Cells(ws + 1, 6) = rq
End If
Next k
End With
Set d = Nothing
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub
|
|