|
代码如下:
Sub 拆分为工作簿() '按条件拆分成表保存为工作簿在同文件夹内
Dim wb As Workbook, arr, rng As Range, d As Object, k, t, sh As Worksheet, i&
Set rng = Range("A1:H1")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
arr = Range("b1:b" & Range("b65536").End(xlUp).Row)
Set d = CreateObject("scripting.dictionary")
For i = 2 To UBound(arr)
' If IsNumeric(arr(i, 1)) Then
If Not d.Exists(arr(i, 1)) Then
Set d(arr(i, 1)) = Cells(i, 1).Resize(1, 4)
Else
Set d(arr(i, 1)) = Union(d(arr(i, 1)), Cells(i, 1).Resize(1, 4))
' End If
End If
Next
k = d.Keys
t = d.Items
For i = 0 To d.Count - 1
Set wb = Workbooks.Add(xlWBATWorksheet)
With wb.Sheets(1)
rng.Copy .Range("A1")
t(i).Copy .Range("A2")
End With
wb.SaveAs Filename:=ThisWorkbook.Path & "\分表\" & k(i) & ".xls"
wb.Close
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "完毕"
End Sub
总表
分开后的表
分开后的表后面四列除表头外没有内容
|
|