|
Sub 拆分()
Application.ScreenUpdating = False
Dim ar As Variant
Dim d As Object
Set d = CreateObject("scripting.dictionary")
Set sht = Sheets("银行收费情况")
Set sh = Sheets("明细")
With Sheets("学生总名单")
r = .Cells(Rows.Count, 1).End(xlUp).Row
If r < 2 Then MsgBox "学生总名单为空!": End
ar = .Range("a1:c" & r)
End With
For i = 2 To UBound(ar)
If Trim(ar(i, 3)) <> "" Then
d(Trim(ar(i, 3))) = ""
End If
Next i
For Each k In d.keys
n = 0
ReDim br(1 To UBound(ar), 1 To 4)
For i = 2 To UBound(ar)
If Trim(ar(i, 3)) = k Then
n = n + 1
br(n, 1) = n
br(n, 2) = ar(i, 2)
br(n, 3) = ar(i, 3)
br(n, 4) = ar(i, 1)
End If
Next i
sh.Copy
Set wb = ActiveWorkbook
With wb.Worksheets(1)
.[a2].Resize(n, 3) = br
.[e2].Resize(n, 1) = Application.Index(br, 0, 4)
End With
sht.Copy after:=wb.Worksheets(wb.Worksheets.Count)
wb.SaveAs Filename:=ThisWorkbook.Path & "\" & k & ".xlsx"
wb.Close
Next k
Application.ScreenUpdating = True
MsgBox "完毕!"
End Sub
|
|