|
为防止重复提交而报错,再加一段.
Sub 保留表头拆分数据为若干新工作簿()
Dim arr, d As Object, k, t, i&, lc%, rng As Range, c%
Application.ScreenUpdating = False
Application.DisplayAlerts = False
r = Cells(Rows.Count, 1).End(3).Row
arr = ThisWorkbook.Sheets("Sheet1").Range("a1:u" & r)
lc = UBound(arr, 2)
Set rng = ThisWorkbook.Sheets("Sheet1").Range("a1:u5") '表头区域
Set d = CreateObject("scripting.dictionary")
For i = 6 To UBound(arr)
If Not d.Exists(arr(i, 2)) Then
Set d(arr(i, 2)) = ThisWorkbook.Sheets("Sheet1").Cells(i, 1).Resize(1, lc)
Else
Set d(arr(i, 2)) = Union(d(arr(i, 2)), ThisWorkbook.Sheets("Sheet1").Cells(i, 1).Resize(1, lc))
End If
Next
k = d.Keys
t = d.Items
For i = 0 To d.Count - 1
mf = Dir(ThisWorkbook.Path & "\*" & k(i) & "*.xls*")
If mf = "" Then
With Workbooks.Add
rng.Copy .Sheets(1).[a1]
t(i).Copy .Sheets(1).[a6]
.SaveAs Filename:=ThisWorkbook.Path & "\" & k(i) & ".xls"
.Close
End With
Else
Set dk = Workbooks.Open(ThisWorkbook.Path & "\" & mf)
dk.Sheets(1).Cells.Clear
rng.Copy dk.Sheets(1).[a1]
t(i).Copy dk.Sheets(1).[a6]
dk.Close True
End If
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "完毕", 64, "提示"
End Sub |
|