|
楼主的代码没有大错误,就是下面红色语句有错,数组arr应该从第一行开始取数,这样就和后面的一致了。
Sub 拆分() '按条件拆分成表保存为工作簿在同文件夹内
Dim wb As Workbook, arr, rng As Range, d As Object, k, t, sh As Worksheet, i&
Set rng = Range("a3:l3")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
arr = Range("b1:b" & Range("b65536").End(xlUp).Row)
Set d = CreateObject("scripting.dictionary")
For Each ws In Worksheets
If ws.Name <> "A" And ws.Name <> "B" Then
ws.Delete
End If
Next
For i = 4 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, 12)
Else
Set d(arr(i, 1)) = Union(d(arr(i, 1)), Cells(i, 1).Resize(1, 12))
' End If
End If
Next
k = d.Keys
t = d.Items
With Sheets
For i = 0 To d.Count - 1
With .Add(after:=.Item(.Count))
.Name = k(i)
rng.Copy .Range("A1")
t(i).Copy .Range("A2")
.Columns("a:l").EntireColumn.AutoFit
.Columns("i:j").ColumnWidth = 6.5
Rows("1:56565").AutoFit
With ActiveSheet.PageSetup
.PrintTitleRows = "$1:$12"
.CenterHeader = "&18原材料、外购外协件检验台账" '页眉/自定义页眉:中(C)
.RightHeader = " №:" & "&P "
.LeftHeader = "HW/JL 8.4-01 " '自定义页眉:左(L)
.LeftMargin = Application.InchesToPoints(0.1) '页边距:左(L)_1.9厘米
.RightMargin = Application.InchesToPoints(0.1) '页边距:右(R)_1.9厘米
.TopMargin = Application.InchesToPoints(1) '页边距:上(T)_2.5厘米
.BottomMargin = Application.InchesToPoints(1) '页边距:下(B)_2.5厘米
.HeaderMargin = Application.InchesToPoints(0.5) '页边距:页眉(A)_1.3厘米
.FooterMargin = Application.InchesToPoints(0.5) '页边距:页脚(F)_1.3厘米
.CenterHorizontally = False '页边距居中方式:水平(Z)
.CenterVertically = False '页边距居中方式:垂直(V)
.Orientation = xlLandscape '页面方向:纵向(T) xlPortrait/ 横向 (L)xlLandscape
.PaperSize = xlPaperA4 '页面纸张大小(Z):A4
.FirstPageNumber = xlAutomatic '页面起始页码:自动
.Zoom = 100 '页面缩放比例:100% 若选择页面缩放比例,则下面两项没有。
End With
End With
Next
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "完毕"
End Sub |
评分
-
1
查看全部评分
-
|