|
Sub test()
Dim ar, br, cr, i&, j&, r&, iStart&, iEnd&, vKey, iPosCol&
Set dic = CreateObject("Scripting.Dictionary")
iStart = [L2]: iEnd = [M2]
br = Range("j2", Cells(Rows.Count, "K").End(3))
If iStart < 1 Or iEnd > UBound(br) Then MsgBox "取值范围有误,请检查!": Exit Sub
With Sheets(1)
cr = Range(.[A7], .Cells(Rows.Count, "AD").End(3))
End With
For k = iStart To iEnd
With Range("A12", Cells(Rows.Count, "I").End(3))
.Offset(1).ClearContents
ar = .Resize(UBound(cr))
For i = 1 To UBound(ar, 2): dic(ar(1, i)) = i: Next
r = 1
End With
vKey = br(k, 2)
For i = 2 To UBound(cr)
If cr(i, UBound(cr, 2)) = vKey Then
r = r + 1
For j = 1 To UBound(cr, 2)
If dic.exists(cr(1, j)) Then
iPosCol = dic(cr(1, j))
ar(r, iPosCol) = cr(i, j)
End If
Next j
End If
Next i
[A12].Resize(r, UBound(ar, 2)) = ar
ActiveSheet.PrintOut
Next k
End Sub |
|