|
楼主 |
发表于 2019-12-17 18:21
|
显示全部楼层
类似这样,自己修改吧。
- Sub test()
- With Sheet1
- c = .Cells.Find("*", , xlFormulas, xlPart, xlByColumns, xlPrevious, False, False, False).Column
- r = .Cells.Find("*", , xlFormulas, xlPart, xlByRows, xlPrevious, False, False, False).Row
- ReDim Height_Array(1 To r)
- ReDim Width_Array(1 To c)
- '获取总表列宽 行高
- For hang = 1 To UBound(Height_Array)
- Height_Array(hang) = .rows(hang).RowHeight
- Next
- For lie = 1 To UBound(Width_Array)
- Width_Array(lie) = .columns(lie).ColumnWidth
- Next
- End With
- '建新表
- '文件复制等
-
- '将获取的行高列宽应用到新表
- Call WidthSetup(Width_Array, ActiveSheet)
- Call HeightSetup(Height_Array, ActiveSheet)
- End Sub
- Sub WidthSetup(Width_Array, sh, Optional startCol = 1) '设置列宽
- For i = LBound(Width_Array) To UBound(Width_Array)
- MyLie = MyLie + 1
- sh.columns(startCol + MyLie - 1).ColumnWidth = Width_Array(i)
- Next
- End Sub
- Sub HeightSetup(Height_Array, sh, Optional startRow = 1) '设置行高
- For j = LBound(Height_Array) To UBound(Height_Array)
- MyHang = MyHang + 1
- sh.rows(startRow + MyHang - 1).RowHeight = Height_Array(j)
- Next
- End Sub
复制代码
另附一个拆分代码,里面进行了列宽设置
- Sub 一表拆成多簿()
- Dim arr
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Application.EnableEvents = False
- zb = ActiveSheet.Name '总表
- Na = ActiveWorkbook.Name
- Na = Left(Na, InStrRev(Na, ".") - 1)
- p = ActiveWorkbook.Path & ""
- Set d = CreateObject("scripting.dictionary")
- '表头行数列数
- hl = InputBox("请输入表头行数和按哪几列拆分,并用逗号隔开,如:1,4,5表示表头有1行、按第4列和第5列进行拆分……", "输入", "1,4,5")
- If hl = "" Then GoTo errhybccdb
- t = Timer
- '新建文件夹'在本工作簿目录下,以本工作簿命名
- If Dir(p & Na, vbDirectory) = "" Then
- MkDir p & Na
- End If
- hl = Replace(hl, ",", ",")
- ro = Val(Split(hl, ",")(0)) '表头行数
- tjs = Split(hl, ",") '拆分条件
- With Sheets(zb) '总表
- col = .Cells.Find("*", , xlFormulas, xlPart, xlByColumns, xlPrevious, False, False, False).Column
- lastr = .Cells.Find("*", , xlFormulas, xlPart, xlByRows, xlPrevious, False, False, False).Row '总表行号和列号
- arr = .[a1].Resize(lastr, col)
- ReDim Width_Array(1 To col)
- For y = 1 To UBound(Width_Array)
- Width_Array(y) = .columns(y).ColumnWidth '总表列宽
- Next
- For i = ro + 1 To UBound(arr)
- tj = ""
- If UBound(tjs) = 0 Then
- MsgBox "请输入拆分条件列!"
- ElseIf UBound(tjs) = 1 Then
- tj = arr(i, tjs(1))
- Else
- For oo = 1 To UBound(tjs)
- tj = tj & arr(i, tjs(oo))
- Next
- End If
- If Not d.exists(tj) Then
- Set d(tj) = .Cells(1, 1).Resize(ro, col)
- End If
- Set d(tj) = Union(d(tj), .Cells(i, 1).Resize(1, col))
- Next
- End With
- '为字典中每个KEY建工作簿
- For Each aa In d.keys
- Set wb = Workbooks.Add
- Set sh = wb.Worksheets(1)
- With sh
- d(aa).Copy .Range("a1")
- ' .Columns.AutoFit '自动列宽
- aa = CLFFZF(aa) '判断文件名是否合法
- .Name = aa '工作表名
- Call WidthSetup(Width_Array, sh)
- End With
- '保存并命名 工作簿名
- wb.SaveAs p & Na & "" & aa & ".xlsx"
- wb.Close
- Next
- MsgBox Format(Timer - t, "已完成,共耗时0.00秒"), vbInformation, "提示"
- tt = p & Na
- Shell "explorer.exe " & [tt], vbNormalFocus
- errhybccdb:
- Application.ScreenUpdating = True
- Application.DisplayAlerts = True
- Application.EnableEvents = True
- End Sub
- Private Function CLFFZF(str) '处理文件名中的非法字符
- ffzfs = "/,\,<,>,*,?,:,"",|" '文件名非法字符集
- ffzfs = Split(Replace(ffzfs, ",", ","), ",")
- For Each ff In ffzfs
- If Len(str) = 0 Then
- str = ""
- ElseIf InStr(str, ff) Then
- str = Replace(str, ff, "-")
- End If
- Next ff
- CLFFZF = str
- End Function
复制代码 |
|