|
Sub 拆分()
Application.ScreenUpdating = False
Dim ar As Variant
Dim d As Object, dc As Object
Set d = CreateObject("scripting.dictionary")
Set sh = ThisWorkbook.Worksheets("sheet1")
With sh
r = .Cells(Rows.Count, 1).End(xlUp).Row
If r < 4 Then MsgBox "数据源为空!": End
ar = .Range("a1:k" & r)
End With
For i = 4 To UBound(ar)
If ar(i, 1) <> "" Then
If Not d.exists(ar(i, 1)) Then Set d(ar(i, 1)) = CreateObject("scripting.dictionary")
d(ar(i, 1))(i) = ""
End If
Next i
For Each k In d.keys
sh.Copy
Set wb = ActiveWorkbook
n = 0
ReDim br(1 To d(k).Count, 1 To UBound(ar, 2))
For Each kk In d(k).keys
n = n + 1
For j = 1 To UBound(ar, 2)
br(n, j) = ar(kk, j)
Next j
Next kk
With wb.Worksheets(1)
.Name = k
.UsedRange.Offset(3).Borders.LineStyle = 0
.UsedRange.Offset(3) = Empty
.[a4].Resize(n, UBound(br, 2)) = br
.[a4].Resize(n, UBound(br, 2)).Borders.LineStyle = 1
End With
wb.SaveAs Filename:=ThisWorkbook.Path & "\" & k & ".xlsx"
wb.Close
Next k
Application.ScreenUpdating = True
MsgBox "ok!", 64, "提醒!"
End Sub
|
|