|
代码如下。。。
Sub 批量AutoFitAll_1()
Application.ScreenUpdating = False
Dim sh As String
Dim MyPath, MyName, AWbName
Dim mysheet As Worksheet
MyPath = ThisWorkbook.Path
MyName = Dir(MyPath & "\" & "*.xls*")
AWbName = ThisWorkbook.Name
Num = 0
Do While MyName <> ""
If MyName <> AWbName Then
Set wb = Workbooks.Open(MyPath & "\" & MyName)
Num = Num + 1
For Each mysheet In wb.Worksheets
With mysheet
For i = 1 To .Cells(.Rows.Count, 1).End(3).Row '.UsedRange.Rows.Count,由于usedrange下面没有用的行过多,改用了以第一列有数据为准,仅供参考
If Application.WorksheetFunction.CountA(.Rows(i)) > 0 Then
.Rows(i).RowHeight = .Rows(i).RowHeight + 4
End If
Next i
r = .Cells(.Rows.Count, 1).End(3).Row
.PageSetup.PrintArea = "$A$1:$H$" & r '以A列有数据为最后的行,H列区域为打印的区域
x = .PageSetup.Pages.Count 'excel工作表打印的总页数
For i = 1 To 100 '最后一页填充满工作表,所以需要一行一行尝试,不超过原来的总页数即可
.PageSetup.PrintArea = "$A$1:$H$" & r + i
y = .PageSetup.Pages.Count
If y > x Then Exit For
Next
.PageSetup.PrintArea = "$A$1:$H$" & r + i - 1
End With
wb.Save
Next mysheet
WbN = WbN & Chr(13) & wb.Name
wb.Close False
End If
MyName = Dir
Loop
MsgBox "共调整" & Num & "个工作薄下全部工作表的行高和列宽。如下:" & Chr(13) & WbN, vbInformation, "提示"
Application.ScreenUpdating = True
End Sub
|
评分
-
1
查看全部评分
-
|