Excel VBA在PDF的单独页面中打印多个命名范围

我有名单的范围和每个范围我把它设置为适合单个页面的列表。 我使用下面的代码导出到PDF,在那里它合并成一个页面。

Dim wbBook As Workbook Dim i As Integer Dim rs As Range Set wbBook = ActiveWorkbook Set rs = wbBook.Names(1).RefersToRange For i = 2 To wbBook.Names.Count Set rs = Union(rs, wbBook.Names(i).RefersToRange) Next rs.ExportAsFixedFormat xlTypePDF, strPath, , , False 

但下面的代码适用于我,当我手动input范围名称。 而我命名的范围是dynamic的。 我认为,上面的代码需要一些修改才能工作。 任何人都可以帮助我完成这个任务吗?

 Set rs = wbBook.Range("Page_1,Page_2,Page_3") rs.Select Selection.ExportAsFixedFormat xlTypePDF, strPath, , , False 

build议的解决scheme仍然给一页。 我在页面设置下面提供,页面设置有问题吗?

 With Sheet1.PageSetup .PrintArea = Range(Cells(iBeginRow, 1), Cells(iRow + 2, 5)).Address .CenterHorizontally = True .CenterVertically = True .Orientation = xlLandscape .LeftMargin = Application.InchesToPoints(0) .RightMargin = Application.InchesToPoints(0) .TopMargin = Application.InchesToPoints(0) .BottomMargin = Application.InchesToPoints(0) .HeaderMargin = Application.InchesToPoints(0) .FooterMargin = Application.InchesToPoints(0) .PrintQuality = 600 .FitToPagesWide = 1 .FitToPagesTall = 1 End With 

然后我dynamic地命名范围

 Sheet1.Range(Cells(iBeginRow, 1), Cells(iRow + 2, 5)).Select Selection.Name = "Page_" & PageNum 

UDF下面为我工作(我在一个模块中):

 Sub ExportRangeToPDF(ByVal WSName As String) Dim oRng As Range Dim strPath As String: strPath = "C:\temp\Temp\" Dim intCount As Integer For intCount = 1 To ActiveWorkbook.Names.Count If InStr(1, LCase(ActiveWorkbook.Names(intCount).RefersToRange.Name.Name), LCase(WSName)) > 0 Then If oRng Is Nothing Then Set oRng = Range(ActiveWorkbook.Names(intCount).RefersToRange.Name.Name) Else Set oRng = Union(oRng, Range(ActiveWorkbook.Names(intCount).RefersToRange.Name.Name)) End If End If Next oRng.ExportAsFixedFormat xlTypePDF, strPath & "test.pdf", , , False Set oRng = Nothing End Sub 

注意 :我在工作簿中有一些“求解器”和“filter数据库”范围,因此必须在这些工作簿之前添加一些validation。 如果您的工作簿中有这样的范围,则可能需要执行相同的操作