|
- Sub test()
- Dim Dict As Object, ar, i As Long, j As Long, p As Long, c As Long, sh As Worksheet
- DoApp False
- For Each sh In Worksheets
- If sh.Name <> "列表" Then sh.Delete
- Next
- Set Dict = CreateObject("Scripting.Dictionary")
- ar = Worksheets(1).Range("A1").CurrentRegion
- ReDim br(1 To UBound(ar), 1 To UBound(ar, 2))
- For j = 1 To UBound(ar, 2)
- br(1, j) = ar(1, j)
- Next
- p = 1 '以第1列为拆分依据
- For i = 2 To UBound(ar)
- If Not Dict.Exists(ar(i, p)) Then Dict(ar(i, p)) = Dict.Count + 1
- Next
- ReDim cr(1 To Dict.Count, 1 To 2)
- For i = 1 To Dict.Count
- cr(i, 1) = 1
- cr(i, 2) = br
- Next
- For i = 2 To UBound(ar)
- c = Dict(ar(i, p))
- cr(c, 1) = cr(c, 1) + 1
- For j = 1 To UBound(ar, 2)
- cr(c, 2)(cr(c, 1), j) = ar(i, j)
- Next
- Next
- For i = 1 To Dict.Count
- With Worksheets.Add(After:=Worksheets(Worksheets.Count))
- .Name = cr(i, 2)(2, p)
- .Range("A1").Resize(cr(i, 1), UBound(ar, 2)) = cr(i, 2)
- .Columns.AutoFit
- End With
- Next
- Worksheets(1).Activate
- Set Dict = Nothing
- DoApp
- Beep
- End Sub
- Function DoApp(Optional b As Boolean = True)
- With Application
- .ScreenUpdating = b
- .DisplayAlerts = b
- If b Then .Calculation = -4105 Else .Calculation = -4135
- End With
- End Function
复制代码 |
|