VBA打印macros以特定的方式设置页面

我一直在研究打印macros,我认为这可能很简单。 我试过录制一个macros,并且一直在研究几个小时看其他人的代码。 我想要的是macros:

1)select活动工作表中的所有单元格

2)设置打印比例以适合所有列到一页

3)打印横向模式

4)打开预览(如果可能的话)

5)如果#4不可行,则执行打印作业。

当我运行我当前的代码时,我的Excel工作表被分割成大量的页面(棋盘式样),然后我得到一个错误代码。 谢谢阅读。

这是我现在的代码:

Sub PrintNOPAsheet()' ' PrintNOPAsheet Macro Cells.Select Application.PrintCommunication = False With ActiveSheet.PageSetup .PrintTitleRows = "" .PrintTitleColumns = "" End With Application.PrintCommunication = True ActiveSheet.PageSetup.PrintArea = "$A$1:$H$346" Application.PrintCommunication = False With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0.25) .RightMargin = Application.InchesToPoints(0.25) .TopMargin = Application.InchesToPoints(0.75) .BottomMargin = Application.InchesToPoints(0.75) .HeaderMargin = Application.InchesToPoints(0.3) .FooterMargin = Application.InchesToPoints(0.3) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .PrintQuality = 600 .CenterHorizontally = False .CenterVertically = False .Orientation = xlLandscape .Draft = False .PaperSize = xlPaperLetter .FirstPageNumber = xlAutomatic .Order = xlDownThenOver .BlackAndWhite = False .Zoom = 100 .PrintErrors = xlPrintErrorsDisplayed .OddAndEvenPagesHeaderFooter = False .DifferentFirstPageHeaderFooter = False .ScaleWithDocHeaderFooter = True .AlignMarginsHeaderFooter = True .EvenPage.LeftHeader.Text = "" .EvenPage.CenterHeader.Text = "" .EvenPage.RightHeader.Text = "" .EvenPage.LeftFooter.Text = "" .EvenPage.CenterFooter.Text = "" .EvenPage.RightFooter.Text = "" .FirstPage.LeftHeader.Text = "" .FirstPage.CenterHeader.Text = "" .FirstPage.RightHeader.Text = "" .FirstPage.LeftFooter.Text = "" .FirstPage.CenterFooter.Text = "" .FirstPage.RightFooter.Text = "" End With Application.PrintCommunication = True Selection.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False End Sub 

这是我通常使用的,然后我使它符合你的问题。 在With您可以从logging的macros中添加尽可能多的属性以适合您的代码。

 Sub printIt() Dim ws As Worksheet: Set ws = Worksheets("Sheet1") Dim rng as Range Dim printRange as String Set rng = ws.Range("A1:J11") ''''For Dynamic Ranges''''' With ws Set rng = .Range(.Range("A1"),.Range("J11").End(xlDown)) End With ''''Range from User Highlighted Cells'''' Set rng = Selection ''''This method is not the best way'''' printRange = ws.Name & "!" & rng.Address With ws.PageSetup .PrintArea = printRange .Zoom = False .FitToPagesWide = 1 'Question 2 .Orientation = xlLandscape 'Question 3 End With ws.PrintOut preview:=True 'Question 4 End Sub