select整个表格进行打印:Excel VBA
我创build了一个VBAmacros来将工作簿的特定工作表导出为PDF。 很简单。 我遇到的问题是,我的代码只selectSheet4
一部分,所以在我的PDF部分表单中缺less。
Sheet4
包含范围A1:W80
中的数据。 但是,运行下面的代码时,只select范围A1:W75
进行打印。 我已经确认我的PrintArea
包含整个页面。 打印时,一切看起来都很棒。
我无休止地寻求解决scheme,没有成功。 这可能是一个页面布局设置问题? 如何确保在导出为PDF格式时select了整个工作表,而不是仅select其中的一部分?
这是我的代码:
Sub SaveReportPDF() Dim filepath As String filepath = "ABC" ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Select Selection.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=filepath, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False End Sub
作为一种良好的做法,您可以将每张纸的使用范围设置为打印区域,并使用以下子文件将其适用于页面:
Sub ScaleForPrinting() Dim sh As Worksheet ' Stop PrintCommunication for speed Application.PrintCommunication = False ' Cycle through each sheet For Each sh In ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")) ' Set print area to used range of sheet sh.PageSetup.PrintArea = sh.UsedRange ' Remove zoom, scale sheet to fit 1 page With sh.PageSetup .CenterHorizontally = True .CenterVertically = True .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = 1 End With Next sh ' Enable PrintCommunication to apply settings Application.PrintCommunication = True End Sub
然后,您将希望select后使用ActiveSheet
对象,而不是Selection
对象。 这可能是反直觉的,但是你想打印表单而不是你在表单中select的表单 。
所以:
ScaleForPrinting ThisWorkbook.Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Select ActiveSheet.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=filepath, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False ' Deselect sheets to avoid nasty multiple sheet accidental editing! ThisWorkbook.Sheets("Sheet1").Select