[英]Excel VBA Print multiple named range in seperate page of PDF
I have list of named ranges and each range I set it to suit single page. 我有命名范围的列表,每个范围都将其设置为适合单个页面。 I use following code to export into PDF, where It get merged together into single page.
我使用以下代码将其导出为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
But below code works for me, when I enter range name manually. 但是,当我手动输入范围名称时,以下代码对我有用。 Whereas my named range are dynamic.
而我的命名范围是动态的。 I think, above code needs some modification to work.
我认为,上面的代码需要进行一些修改才能起作用。 Will anyone assist me to get this done?
有人会协助我完成这项工作吗?
Set rs = wbBook.Range("Page_1,Page_2,Page_3")
rs.Select
Selection.ExportAsFixedFormat xlTypePDF, strPath, , , False
Proposed solution still give one page. 提议的解决方案仍然给出一页。 I provided below the page setup, Is something wrong with page setup ?
我在页面设置下面提供了页面设置有问题吗?
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
Then I dynamically name the range 然后我动态命名范围
Sheet1.Range(Cells(iBeginRow, 1), Cells(iRow + 2, 5)).Select
Selection.Name = "Page_" & PageNum
Below UDF worked for me (I had it in a module): 下面的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
NOTE : I had some 'solver' and 'filterdatabase' ranges in the workbook I was working on so had to add some validation for those before it worked. 注意 :我正在工作的工作簿中有一些“求解器”和“ filterdatabase”范围,因此必须在工作之前为其添加一些验证。 If you have any such ranges in your workbook, you might have to do the same
如果您的工作簿中有任何此类范围,则可能必须执行相同的操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.