使用VBA将两个不同工作表上的多个范围保存为PDF

时间:2017-11-03 15:51:24

标签: excel vba excel-vba pdf

我需要将这两张表和范围合并为一个PDF。我已经尝试了所有可以找到的宏,但没有一个能够工作。这是我正在使用的宏,除了在一个Doc

中组合的范围之外,它们都有效
Private Sub SaveLHForms()

Application.ScreenUpdating = False
Application.DisplayAlerts = False

FormName = Sheets("SETUP").Range("B2").Value & " " & ActiveSheet.Range("S1") & ".pdf"

    ChDir DesktopAddress
    Sheets("Lienholder Docs").Range("A45:I151").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        FormName, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
       False
    Sheets("Settlement Letters").Range("A47:I92").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        FormName, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
        False

Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub

2 个答案:

答案 0 :(得分:1)

这可能有效:

Sub Macro1()

   Sheets("Lienholder Docs").Activate
   ActiveSheet.Range("A45:I151").Select
   Sheets("Settlement Letters").Activate
   ActiveSheet.Range("A47:I92").Select

   Sheets(Array("Lienholder Docs", "Settlement Letters")).Select

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

基于:

Excel VBA to Export Selected Sheets to PDF

修改#1:

此版本应取消任何分组:

Sub Macro2()
    Dim s As Worksheet
    Set s = ActiveSheet
    FormName = "C:\TestFolder\xxx.pdf"

    Sheets("Lienholder Docs").Activate
    ActiveSheet.Range("A45:I151").Select
    Sheets("Settlement Letters").Activate
    ActiveSheet.Range("A47:I92").Select

   Sheets(Array("Lienholder Docs", "Settlement Letters")).Select

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

    s.Activate
End Sub

答案 1 :(得分:0)

首先,您需要在每张纸的页面设置中设置打印区域后创建一个pdf文档。

Sub test()
    Dim path As String
    Dim myArr As Variant, a As Variant
    Dim rngArr As Variant
    Dim Ws As Worksheet
    Dim formName As String
    Dim i As Integer

    formName = Sheets("SETUP").Range("B2").Value & " " & ActiveSheet.Range("S1") & ".pdf"

    myArr = Array("Lienholder Docs", "Settlement Letters") '<~~ Sheet name
    rngArr = Array("A45:I151", "A47:I92") '<~~ print area address

    For i = 0 To UBound(myArr)
        Set Ws = Sheets(myArr(i))
        With Ws
            .PageSetup.PrintArea = .Range(rngArr(i)).Address
        End With
    Next a
    Sheets(myArr).Select

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


End Sub