将XCEL文件按某例拆分文件,并保留两行表头。如何保持原表的列宽、行高不变?
将XCEL文件按某例拆分文件,并保留两行表头。如何保持原表的列宽、行高不变?原代码和附件如下,请教,如何修改?
表格拆分测试文件.rar
(17.75 KB)
Sub 保留2行表头拆分数据为若干新文件()
Dim arr, d As Object, k, t, i&, lc%, rng As Range, c%
c = Application.InputBox("请输入拆分列号", , 1, , , , , 1)
If c = 0 Then Exit Sub
Application.ScreenUpdating = False
Application.DisplayAlerts = False
arr = [a1].CurrentRegion
lc = UBound(arr, 2)
Set rng = [a1].Resize(2, lc) '保留2行表头
Set d = CreateObject("scripting.dictionary")
For i = 3 To UBound(arr) '从第3行往下复制
If Not d.Exists(arr(i, c)) Then
Set d(arr(i, c)) = Cells(i, 1).Resize(1, lc)
Else
Set d(arr(i, c)) = Union(d(arr(i, c)), Cells(i, 1).Resize(1, lc))
End If
Next
k = d.Keys
t = d.Items
For i = 0 To d.Count - 1
With Workbooks.Add(xlWBATWorksheet)
rng.Copy .Sheets(1).[a1]
t(i).Copy .Sheets(1).[a3] '正文从A3往下粘贴
.SaveAs Filename:=ThisWorkbook.Path & "\" & k(i) & ".xls"
.Close
End With
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "完毕"
End Sub