2015-11-02 90 views
2

我有一个报告,我试图用VBA脚本打印。我需要在一个页面(肖像)上打印报告并使其居中。我有格式化下来,但范围导致我的问题。我需要设置打印范围以适应指定范围内的值的数量。现在,它将打印指定范围,但如果添加或删除了条目,它将不会调整或打印扩展或缩小的范围。打印一个动态命名范围

Sub PrintFailureReport() 

Sheets("Failure Report").Activate 
Range("FailReportPrintArea").Select 
ActiveSheet.PageSetup.PrintArea = Selection.Range("FailReportPrintArea").Address 
Application.PrintCommunication = True 

With ActiveSheet.PageSetup 
     .LeftMargin = Application.InchesToPoints(0.25) 
     .RightMargin = Application.InchesToPoints(0.25) 
     .TopMargin = Application.InchesToPoints(0.25) 
     .BottomMargin = Application.InchesToPoints(0.25) 
'   .HeaderMargin = Application.InchesToPoints(0.3) 
'   .FooterMargin = Application.InchesToPoints(0.3) 
     .PrintGridlines = False 
     .CenterHorizontally = True 
     .CenterVertically = True 
     .Orientation = xlPortrait 
     .PaperSize = xlPaperLetter 
     .Zoom = False 
     .FitToPagesWide = 1 
     .FitToPagesTall = 1 
     .BlackAndWhite = True 
End With 

Range("FailReportPrintArea").PrintOut 

End Sub 
+0

先重新命名的范围是多少? – findwindow

+1

将列更改为您需要的列,这将命名范围从A1到列“C”,“范围(单元格(1,”A“),单元格(Rows.Count,”C“)中的最后一行。 End(xlUp))。Name =“FailReportPrintArea”' – Davesexcel

回答

0

我认为这与没有添加Application.PrintCommunication = False做之前你设定的地址

Sub PrintFailureReport() 

Sheets("Failure Report").Activate 
Range("FailReportPrintArea").Select 
Application.PrintCommunication = False 
ActiveSheet.PageSetup.PrintArea = Selection.Range("FailReportPrintArea").Address 
Application.PrintCommunication = True 

With ActiveSheet.PageSetup 
     .LeftMargin = Application.InchesToPoints(0.25) 
     .RightMargin = Application.InchesToPoints(0.25) 
     .TopMargin = Application.InchesToPoints(0.25) 
     .BottomMargin = Application.InchesToPoints(0.25) 
'   .HeaderMargin = Application.InchesToPoints(0.3) 
'   .FooterMargin = Application.InchesToPoints(0.3) 
     .PrintGridlines = False 
     .CenterHorizontally = True 
     .CenterVertically = True 
     .Orientation = xlPortrait 
     .PaperSize = xlPaperLetter 
     .Zoom = False 
     .FitToPagesWide = 1 
     .FitToPagesTall = 1 
     .BlackAndWhite = True 
End With 

Range("FailReportPrintArea").PrintOut 

End Sub