|
Sub 拆分()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim arr, d As Object, k, lc%, rng As Range
Dim rng2 As Range
For Each sh In Sheets
If sh.Index > 1 Then
sh.Delete
End If
Next sh
arr = Sheet1.[a1].CurrentRegion
lc = UBound(arr, 2)
Set rng = Sheet1.[a1].Resize(, lc)
Set d = CreateObject("scripting.dictionary")
For i = 2 To UBound(arr)
If Not d.Exists(arr(i, 1)) Then
Set d(arr(i, 1)) = Cells(i, 1).Resize(1, lc)
Else
Set d(arr(i, 1)) = Union(d(arr(i, 1)), Cells(i, 1).Resize(1, lc))
End If
Next
k = d.Keys
Application.SheetsInNewWorkbook = 1 '设置创建2个工作表
FileName1 = ThisWorkbook.Path & "\" & ThisWorkbook.Name
For i = 0 To UBound(k)
Set sht = Worksheets.Add(after:=Sheets(Sheets.Count))
rng.Copy sht.[a1]
d.items()(i).Copy sht.[a2]
sht.Name = k(i)
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "拆分结束"
End Sub
|
|