Excel VBA 在 PDF 的单独页面中打印多个命名范围
Excel VBA Print multiple named range in seperate page of 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
但是当我手动输入范围名称时,下面的代码对我有用。而我的命名范围是动态的。我认为,上面的代码需要一些修改才能工作。有人能帮我完成这件事吗?
Set rs = wbBook.Range("Page_1,Page_2,Page_3")
rs.Select
Selection.ExportAsFixedFormat xlTypePDF, strPath, , , False
建议方案还是给一页。我在页面设置下面提供了,页面设置有问题吗?
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
然后我动态命名范围
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
注意:我正在处理的工作簿中有一些 'solver' 和 'filterdatabase' 范围,因此必须在它起作用之前为它们添加一些验证.如果您的工作簿中有任何此类范围,您可能必须执行相同的操作
我有命名范围列表,我将每个范围设置为适合单页。我使用以下代码导出为 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
但是当我手动输入范围名称时,下面的代码对我有用。而我的命名范围是动态的。我认为,上面的代码需要一些修改才能工作。有人能帮我完成这件事吗?
Set rs = wbBook.Range("Page_1,Page_2,Page_3")
rs.Select
Selection.ExportAsFixedFormat xlTypePDF, strPath, , , False
建议方案还是给一页。我在页面设置下面提供了,页面设置有问题吗?
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
然后我动态命名范围
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
注意:我正在处理的工作簿中有一些 'solver' 和 'filterdatabase' 范围,因此必须在它起作用之前为它们添加一些验证.如果您的工作簿中有任何此类范围,您可能必须执行相同的操作