【问题标题】:Excel VBA Export Multiple sheets to PDFExcel VBA 将多张工作表导出为 PDF
【发布时间】:2016-08-04 11:55:14
【问题描述】:

我一直在尝试将多个工作表上的命名范围导出到单个 pdf。最初,它似乎工作正常,然后我注意到将各种工作表加载到数组中时,所选范围会发生变化。

我的理解是使用Selection.ExportAsFixedFormat 这只会导出选定的单元格。因此,我有一个宏来遍历所需的工作表,选择所需的各种范围,然后为所有工作表创建一个数组以允许导出到单个 pdf。

Dim wb As Workbook
Dim srcSht As Worksheet
Dim toPrnt As String

Set wb = ThisWorkbook
Set srcSht = wb.Sheets("print_array")

Dim myArr1() As Variant
    myArr1 = srcSht.Range("myPrintArray")

Dim i As Long
Dim j As String
    For i = LBound(myArr1, 1) To UBound(myArr1, 1)
            j = myArr1(i, 1)

    wb.Sheets(j).Activate
        wb.ActiveSheet.Range("CCB_" & Left(j, 5) & "_Print").Select

    Next i

wb.Sheets(Array("CAT01 - Request for Resource", _
                            "CAT02 - Resource Allocation", _
                            "CAT03 - Product Data Sources", _
                            "CAT04 - Target & Control Cells", _
                            "CAT05 - Matching And Deduping", _
                            "CAT06 - Exclusions", _
                            "CAT07 - Data from other teams", _
                            "CAT08 - Outputs", _
                            "CAT09 - Special Instructions", _
                            "CAT10 - Brief Meeting Sign Off" _
                            )).Select

Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDF_FileAndPath, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True

单步执行代码时,一切都按计划进行,直到创建工作表数组,此时所选范围发生变化。

我也尝试过使用PageSetup,但结果是一样的。

With ActiveSheet.PageSetup
    .Orientation = xlLandscape
    .PaperSize = xlPaperA4
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = 1
    .PrintArea = Range("CCB_" & Left(j, 5) & "_Print").Address
End With

在这个论坛上看了几篇类似的帖子,我还是一头雾水。

任何人都可以解释为什么在创建数组时所选范围会发生变化,或者有任何其他可能有帮助的建议吗?

非常感谢

【问题讨论】:

    标签: vba excel pdf excel-2010


    【解决方案1】:

    我遇到了同样的问题。我遇到了另一个更简单的解决方案。不需要临时文件。

    在 .select 数组后,使用 ActiveSheet.ExportAsFixedFormat 而不是 Selection.ExportAsFixedFormat。因为您的工作表是通过初始 .select 激活的,所以 ActiveSheet 会选择所有想要导出的工作表。

    您的代码应如下所示:

    Dim wb As Workbook
    Dim srcSht As Worksheet
    Dim toPrnt As String
    
    Set wb = ThisWorkbook
    Set srcSht = wb.Sheets("print_array")
    
    Dim myArr1() As Variant
        myArr1 = srcSht.Range("myPrintArray")
    
    Dim i As Long
    Dim j As String
        For i = LBound(myArr1, 1) To UBound(myArr1, 1)
                j = myArr1(i, 1)
    
        wb.Sheets(j).Activate
            wb.ActiveSheet.Range("CCB_" & Left(j, 5) & "_Print").Select
    
        Next i
    
    wb.Sheets(Array("CAT01 - Request for Resource", _
                                "CAT02 - Resource Allocation", _
                                "CAT03 - Product Data Sources", _
                                "CAT04 - Target & Control Cells", _
                                "CAT05 - Matching And Deduping", _
                                "CAT06 - Exclusions", _
                                "CAT07 - Data from other teams", _
                                "CAT08 - Outputs", _
                                "CAT09 - Special Instructions", _
                                "CAT10 - Brief Meeting Sign Off" _
                                )).Select
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDF_FileAndPath, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True
    

    【讨论】:

      【解决方案2】:

      我设法通过将选定的范围复制到一个临时文件然后从那里导出来解决我的问题。完整的解决方案如下所示...

      Dim wb As Workbook
      Dim srcSht As Worksheet
      Dim tempFile As String
      
      Set wb = ThisWorkbook
      Set srcSht = wb.Sheets("print_array")
      
      Dim myArr1() As Variant
          myArr1 = srcSht.Range("myPrintArray")
      
      Dim i As Long
      Dim j As String
          For i = LBound(myArr1, 1) To UBound(myArr1, 1)
                  j = myArr1(i, 1)
      
          wb.Sheets(j).Activate
      
          With ActiveSheet.PageSetup
              .Orientation = xlLandscape
              .PaperSize = xlPaperA4
              .Zoom = False
              .FitToPagesWide = 1
              .FitToPagesTall = 1
              .PrintArea = Range("CCB_" & Left(j, 5) & "_Print").Address
          End With
      
              Next i
      
          wb.Sheets(Array("CAT01 - Request for Resource", _
                                      "CAT02 - Resource Allocation", _
                                      "CAT03 - Product Data Sources", _
                                      "CAT04 - Target & Control Cells", _
                                      "CAT05 - Matching And Deduping", _
                                      "CAT06 - Exclusions", _
                                      "CAT07 - Data from other teams", _
                                      "CAT08 - Outputs", _
                                      "CAT09 - Special Instructions", _
                                      "CAT10 - Brief Meeting Sign Off" _
                                      )).copy
      
                      With ActiveWorkbook
                          .Save
      
                          tempFile = .FullName
      
                          .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDF_FileAndPath, _
                              Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
                              :=False, OpenAfterPublish:=True
      
                          .Close
      
                      End With
      
                      Kill tempFile
      
         End If
      

      我希望这对将来的某人有所帮助。

      谢谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-14
        • 2021-04-22
        • 1970-01-01
        • 2014-01-12
        • 2012-10-16
        • 2020-06-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多