选择单元格范围,然后打印

时间:2016-08-09 05:47:45

标签: excel-vba vba excel

我有以下代码可以正常工作:

Sheets("File1").Select
Range("B2:J56").Select
Selection.PrintOut Copies:=1, Collate:=True

但我需要打印多个范围,页面应符合以下格式:

  • 每个选择都应该适合A4纸
  • 双面
  • 在一个页面中调整列,以便内容完全适合页面宽度
  • 600 dpi的质量打印
  • 边距居中(水平和垂直)
  • 页面应包含标题“此数据是在一年前收集的”

在互联网上挣扎无济于事之后,我录制了一个宏,但是当我尝试重现代码片段时仍然存在很多错误,  特别是时间执行错误#1004 在行ActiveSheet.PageSetup.PrintArea = "$B$2:$J$56;$L$2:$T$37;$V$2:$AH$37"打印通信方法失败

[编辑]:解决方案在

下面实施
 Sub RECORDED_Macro_Print_test()

     Application.ScreenUpdating = False

         Sheets("File1").Select
         Range("B2:J83,L2:T37,V2:AO92").Select
         Range("V2").Activate
         ActiveSheet.PageSetup.PrintArea = "$B$2:$J$83;$L$2:$T$37;$V$2:$AO$92" ' <----- Fix it by replacing ';' by ',' as suggested by the user 'Siva' 
         Application.PrintCommunication = False
         With ActiveSheet.PageSetup
             .PrintTitleRows = ""    ' <----- Not necessary
             .PrintTitleColumns = "" ' <----- Not necessary
             .LeftHeader = ""        ' <----- Not necessary
             .CenterHeader = "This data was collected a year ago"
             .RightHeader = ""       ' <----- Not necessary
             .LeftFooter = ""        ' <----- Not necessary
             .CenterFooter = ""      ' <----- Not necessary
             .RightFooter = ""       ' <----- Not necessary
             .LeftMargin = Application.InchesToPoints(0.511811023622047)
             .RightMargin = Application.InchesToPoints(0.511811023622047)
             .TopMargin = Application.InchesToPoints(0.78740157480315)
             .BottomMargin = Application.InchesToPoints(0.78740157480315)
             .HeaderMargin = Application.InchesToPoints(0.31496062992126)
             .FooterMargin = Application.InchesToPoints(0.31496062992126)
             .PrintHeadings = False
             .PrintGridlines = False
             .PrintComments = xlPrintNoComments
             .PrintQuality = 600
             .CenterHorizontally = True
             .CenterVertically = True
             .Orientation = xlPortrait
             .Draft = False
             .PaperSize = xlPaperA4
             .FirstPageNumber = xlAutomatic
             .Order = xlDownThenOver
             .BlackAndWhite = False
             .Zoom = False
             .FitToPagesWide = 1
             .FitToPagesTall = False
             .PrintErrors = xlPrintErrorsDisplayed
             .OddAndEvenPagesHeaderFooter = False
             .DifferentFirstPageHeaderFooter = False
             .ScaleWithDocHeaderFooter = True
             .AlignMarginsHeaderFooter = True
              .EvenPage.LeftHeader.Text = ""    ' <----- Not necessary
              .EvenPage.CenterHeader.Text = ""  ' <----- Not necessary
              .EvenPage.RightHeader.Text = ""   ' <----- Not necessary
              .EvenPage.LeftFooter.Text = ""    ' <----- Not necessary
              .EvenPage.CenterFooter.Text = ""  ' <----- Not necessary
              .EvenPage.RightFooter.Text = ""   ' <----- Not necessary
              .FirstPage.LeftHeader.Text = ""   ' <----- Not necessary
              .FirstPage.CenterHeader.Text = "" ' <----- Not necessary
              .FirstPage.RightHeader.Text = ""  ' <----- Not necessary
              .FirstPage.LeftFooter.Text = ""   ' <----- Not necessary
              .FirstPage.CenterFooter.Text = "" ' <----- Not necessary
              .FirstPage.RightFooter.Text = ""  ' <----- Not necessary
         End With
         Application.PrintCommunication = True  ' <----- Remove this line to fix it. This is an error issue
         Selection.PrintOut Copies:=1, Collate:=True
     End Sub

要记录宏我已经基本上按住了SHIFT键选择了我需要的所有范围,然后我定义了打印区域,然后按下了print,在对话框中指定了所有约束并vo。如何让它工作?

1 个答案:

答案 0 :(得分:1)

替换您的代码行

ActiveSheet.PageSetup.PrintArea = "$B$2:$J$56;$L$2:$T$37;$V$2:$AH$37"

ActiveSheet.PageSetup.PrintArea = "$B$2:$J$56,$L$2:$T$37,$V$2:$AH$37"

我已经在我的机器上尝试过了,并且替换

后该行没有出现任何错误
相关问题