在单独的Excel VBA工作表上将多个命名范围打印为1 PDF

在单独的Excel VBA工作表上将多个命名范围打印为1 PDF,excel,vba,Excel,Vba,我没有多张工作表,而是希望打印到pdf的不同范围。如何根据shipYesNo=“是”或“否”打印范围?我正在努力找出如何组合PDF命名范围的多个导出(与我已经看到的表单相比)。在最底层,我试图将这些陈述结合起来。我也尝试过结合,但没有成功 状态打印和发货详情是我指的两个范围 Public Function findLastRow(col As Range) As Long 'making the macro available to all Sub Procedures Dim

我没有多张工作表,而是希望打印到pdf的不同范围。如何根据shipYesNo=“是”或“否”打印范围?我正在努力找出如何组合PDF命名范围的多个导出(与我已经看到的表单相比)。在最底层,我试图将这些陈述结合起来。我也尝试过结合,但没有成功

状态打印和发货详情是我指的两个范围

Public Function findLastRow(col As Range) As Long 'making the macro available to all Sub Procedures

        Dim lastRow As Long
        With col.Parent
            lastRow = .Cells(.Rows.Count, col.Column).End(xlUp).Row 'finding the last possible row disregarding blanks
        End With

        findLastRow = lastRow

End Function

Public Sub Print_StatusPDF()

Dim Status_Print As Range
Dim Ship_Detail As Range
Dim Cust_Name As Range
Dim daterng As Range
Dim strFileName As String
Dim strFileShipments As String
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim FilePath As String
Dim PrintSheets As Range

Set ws1 = Sheets("Order_Status")
Set ws2 = Sheets("Shipment_Detail")
Set Status_Print = ws1.Range("B2:N" & findLastRow(ws1.Range("B9")))
Set Ship_Detail = ws2.Range("B1:G" & findLastRow(ws2.Range("G6")))
Set Cust_Name = Range("C5")
Set daterng = Sheets("Names").Range("date_range")
strFileName = Cust_Name & " Order Status " & daterng
strFileShipments = Cust_Name & " Shipment Detail "

'Setting the defined name range for printing
ThisWorkbook.Names.Add Name:="Order_Print", RefersTo:=Status_Print
ThisWorkbook.Names.Add Name:="Ship_Print", RefersTo:=Ship_Detail

'Autofitting the columns and setting them to center alignment
'Adjust this if columns are deleted/inserted
Range("B:M").Columns.AutoFit
Range("E:M").Columns.HorizontalAlignment = xlLeft

ws2.Range("B:H").Columns.AutoFit

'Setting the page setup options for printing
With Sheets("Order_Status").PageSetup
    .Orientation = xlLandscape
    .PrintArea = ws1.Range("Order_Print").Address
    .FitToPagesWide = 1
    .FitToPagesTall = False
    .Zoom = False
    .LeftMargin = Application.InchesToPoints(0.36)
   .RightMargin = Application.InchesToPoints(0.25)
   .TopMargin = Application.InchesToPoints(0.5)
   .BottomMargin = Application.InchesToPoints(0.5)
   .HeaderMargin = Application.InchesToPoints(0.25)
   .FooterMargin = Application.InchesToPoints(0.25)
    
End With

With ws2.PageSetup
    .Orientation = xlPortrait
    .PrintArea = ws2.Range("Ship_Print").Address
    .FitToPagesWide = 1
    .FitToPagesTall = False
    .Zoom = False
    .LeftMargin = Application.InchesToPoints(0.36)
   .RightMargin = Application.InchesToPoints(0.25)
   .TopMargin = Application.InchesToPoints(0.5)
   .BottomMargin = Application.InchesToPoints(0.5)
   .HeaderMargin = Application.InchesToPoints(0.25)
   .FooterMargin = Application.InchesToPoints(0.25)
    
End With


FilePath = CreateObject("WScript.Shell").specialfolders("Desktop")
Debug.Print FilePath


'PrintSheets = Union(Status_Print, Ship_Detail)

'setting the location and name for where to print
'if shipYesNo = No then there's only the Staus_Print to print
If Sheets("Names").Range("shipYesNo").Value = "No" Then
Status_Print.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:= _
    FilePath & "\" & strFileName & ".pdf" _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True
    
'if shipYesNo = Yes then there are 2 ranges to print, with Status Print being on page 1 and Ship Detail on page 2
ElseIf Sheets("Names").Range("shipYesNo").Value = "Yes" Then:

Status_Print.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:= _
    FilePath & "\" & strFileName & ".pdf" _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True

Ship_Detail.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:= _
    FilePath & "\" & strFileShipments & ".pdf" _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=True
End If

End Sub

你能走另一条路吗?用另一张工作表在一个数据块中列出所有数据的副本,然后只打印一个数据块?有没有办法将这些数据块分开,使它们分别放在一页上?