【问题标题】:vba - printing worksheets in workbooks in foldervba - 在文件夹中的工作簿中打印工作表
【发布时间】:2016-10-03 19:22:07
【问题描述】:

用wb

Set Sh2 = .Sheets("sheet2)
    With Sh2.PageSetup
        .PrintArea = "$B$2:$S$80"
        .PaperSize = xlPaperLegal
    End With

Set Sh3 = .Sheets("sheet3")
    With Sh3.PageSetup
        .PrintArea = "$B$2:$M$104"
        .PaperSize = xlPaperLegal
        .Orientation = xlPortrait
    End With

Set execsum1 = .Sheets("sheet4")
    With execsum1.PageSetup
        .PrintArea = "$B$7:$N$63"
        .PaperSize = xlPaperLegal
        .Orientation = xlLandscape
        .PrintTitleRows = "$B$2:$N$6"
    End With

Set execsum2 = .Sheets("sheet5")
    With execsum2.PageSetup
        .PrintArea = "$B$64:$N$106"
        .PaperSize = xlPaperLegal
        .Orientation = xlLandscape
        .PrintTitleRows = "$B$2:$N$6"
    End With
    'ActiveSheet.PrintPreview

Set noi1 = .Sheets("sheet6")
    With noi1.PageSetup
        .PrintArea = "$B$10:$N$44"
        .PaperSize = xlPaperLegal
        .Orientation = xlLandscape
        .PrintTitleRows = "$B$2:$N$8"
        .FitToPagesTall = 1
    End With

Set noi2 = .Sheets("sheet7")
    With noi2.PageSetup
        .PrintArea = "$B$46:$N$192"
        .PaperSize = xlPaperLegal
        .Orientation = xlLandscape
        .PrintTitleRows = "$B$2:$N$8"
        '.FitToPagesWide = 1
        .FitToPagesTall = 1
    End With

End With

Dim sheet As Variant
For Each sheet In Array(execsum1, execsum2, Sh2, Sh3, noi1, noi2)
    sheet.PrintOut Copies:=1
Next

    'Save and Close Workbook
      'wb.Close SaveChanges:=False

    'Ensure Workbook has closed before moving on to next line of code
      DoEvents

    'Get next file name
      myFile = Dir
  Loop

'Message Box when tasks are completed
  MsgBox "Task Complete!"

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub

大家好,我是 VBA 新手,我正在尝试为文件夹中的每个工作簿打印 6 个工作表/页面。 Execsum1 和 execsum2 来自具有不同打印区域的同一个工作表; noi1 和 noi2 的故事相同。当我运行代码时,它会两次打印出第二个指定页面(execsum2 和 noi2)。为什么 execsum1/noi1 没有打印出来,如果可能的话,我怎样才能使代码更有效率?谢谢。

【问题讨论】:

    标签: vba excel printing


    【解决方案1】:

    它会打印同一张工作表两次,因为在将 PageSetup 从第一个版本更改为第二个版本之间没有打印工作表。你在这里收集工作表参考...

    Set execsum1 = .Sheets("Exec Summary")
    '...
    Set execsum2 = .Sheets("Exec Summary")
    

    ...彼此相同。一个工作表只有 1 个PageSetup,所以当你这样做时......

    For Each sheet In Array(execsum1, execsum2, Sh2, Sh3, noi1, noi2)
        sheet.PrintOut Copies:=1
    Next
    

    ...你得到了它设置的最后一个东西。

    完全跳过循环并单独打印每个循环。循环遍历它们绝对没有任何好处。

    With execsum1.PageSetup
        .PrintArea = "$B$7:$N$63"
        .PaperSize = xlPaperLegal
        .Orientation = xlLandscape
        .PrintTitleRows = "$B$2:$N$6"
    End With
    execsum1.PrintOut Copies:=1   '<--- After each With block.
    

    如果您想简化代码,只需将常见的 .PageSetup 提取到一个函数中,并将其他所有内容作为参数传递(请注意,这只是一个示例 - 我没有包含您使用的所有内容)。即:

    Private Sub PrintCustomRange(sheet As Worksheet, area As String, title As String, _
                                 orient As XlPageOrientation, paper As XlPaperSize)
        With sheet.PageSetup
            .PrintArea = area
            .PaperSize = paper
            .Orientation = orient
            If Len(title) > 0 Then .PrintTitleRows = title
        End With
        .PrintOut Copies:=1
    End Sub
    

    然后这样称呼它:

    PrintCustomRange Sheets("Proforma NOI"), "$B$46:$N$192", "$B$2:$N$8", xlLandscape, xlPaperLegal
    

    【讨论】:

    • @wowmoo - 这听起来像是与打印机驱动程序有关。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-08
    • 2022-12-14
    • 1970-01-01
    • 2018-05-27
    相关资源
    最近更新 更多