|
楼主 |
发表于 2017-3-7 20:57
|
显示全部楼层
按班级导出的xls文件能保持原文件的列宽格式吗?敬请老师们赐教。
http://club.excelhome.net/thread-1332550-1-1.html
(出处: ExcelHome技术论坛)
将原代码稍作修改即可满足要求:
Sub 按班保存为工作簿()
Dim wb As Workbook, arr, rng As Range, d As Object, k, t, sh As Worksheet, i&
Set rng = Range("A1:x3")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
arr = Range("a1:x" & Range("a65536").End(xlUp).Row)
Set d = CreateObject("scripting.dictionary")
For i = 4 To UBound(arr)
If Not d.exists(arr(i, 1)) Then
Set d(arr(i, 1)) = Cells(i, 1).Resize(1, 24)
Else
Set d(arr(i, 1)) = Union(d(arr(i, 1)), Cells(i, 1).Resize(1, 24))
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 .[A1]: t(i).Copy .[A4]
For j = 1 To UBound(arr, 2)
.Columns(j).ColumnWidth = ThisWorkbook.ActiveSheet.Columns(j).ColumnWidth
Next
End With
wb.SaveAs Filename:=ThisWorkbook.Path & "\各班成绩文件\" & Mid(Range("f1"), 5, 7) & k(i) & "班成绩", FileFormat:=xlExcel8
wb.Close
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "完毕"
End Sub |
|