我认为范围限制为255个字符,因此我将范围分为6个单元格 B1到B6(下面的单元格B1和B2的示例都在255个字符以下)。
A1:I15, A17:I40, A42:I65, A92:I114, A116:I140, A142:I168, A170:I196, A198:I224, A226:I252, A254:I280, A282:I308, A310:I336, A338:I364, A366:I392, A394:I420, A422:I448
A450:I476, A478:I504, A526:I552, A554:I580, A582:I608, A610:I636, A638:I664, A666:I690, A692:I707, A730:I750, A752:I773, A775:I794, A796:I815, A817:I830, A855:I877, A879:I905, A907:I926
我尝试使用Union函数从这些范围生成PDF,但是不知何故我只能从B1获取范围! B2被忽略。这是我的代码:
Set rng = Union(shTemp.Range("B1"), shTemp.Range("B2"))
shTransformed.Activate
With ActiveSheet.PageSetup
.Zoom = False
.Orientation = xlPortrait
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintArea = rng
End With
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="c:\temp\test.pdf", _
Quality:=xlQualityStandard, _
IgnorePrintAreas:=False, _
IncludeDocProperties:=True, _
OpenAfterPublish:=True
答案 0 :(得分:2)
通过添加水平分页符并隐藏打印区域之间的行,可以使用此替代方法绕过255个字符限制的打印区域范围地址。但是,这仅在这种情况下适用,因为每个打印区域中最右边的列是相同的(即I),并且此方法还要求每个打印区域至少要分隔一行。
已成功使用OP中定义的范围字符串对其进行了测试。对表格名称,范围等进行一些修改。
Sub test()
Dim shTemp As Worksheet, shTr As Worksheet
Dim HideRng As Range, Rng As Range, MainRng As Range
Dim Ar As Range, cel As Range
Set shTemp = ThisWorkbook.Sheets(1)
Set shTr = ThisWorkbook.Sheets(2)
'To Dynamically Select Range containing Addresses
Dim SelRng As Range
Set SelRng = shTemp.Range("B1:B6") ' Default range
shTemp.Activate
On Error Resume Next
Set SelRng = Application.InputBox("Select the range containing Print Range Addresses", "Select Range", SelRng.Address, , , , , 8)
If Err > 0 Then
Err.Clear
Exit Sub
End If
On Error GoTo 0
If SelRng Is Nothing Then Exit Sub
For Each cel In SelRng.Cells
If cel.Value <> "" Then
If Not Range(cel.Value) Is Nothing Then
'Debug.Print Range(cel.Value).Address
If Rng Is Nothing Then
Set Rng = Range(cel.Value)
Else
Set Rng = Union(Rng, Range(cel.Value))
End If
End If
End If
Next
If Rng Is Nothing Then Exit Sub
With shTr
.Cells.PageBreak = xlPageBreakNone
pg = 1
maxcol = 1
For Each Ar In Rng.Areas
'Vartical Pagebreak: it is applicable only in this case where right column is same
If pg = 1 Then
Set MainRng = Ar(1, 1)
.VPageBreaks.Add Ar(1, Ar.Columns.Count).Offset(0, 1)
End If
'Ar(1, 1).Value = "Page " & pg
.HPageBreaks.Add Ar(Ar.Rows.Count, Ar.Columns.Count).Offset(1, 0)
If pg > 1 Then
If HideRng(HideRng.Rows.Count, 1).Row < Ar(1, 1).Row Then
Set HideRng = Range(HideRng, Ar(1, 1).Offset(-1, 0))
HideRng.EntireRow.Hidden = True
End If
End If
Set HideRng = Ar(Ar.Rows.Count, 1).Offset(1, 0)
If pg = Rng.Areas.Count Then Set MainRng = Range(MainRng, Ar(Ar.Rows.Count, Ar.Columns.Count))
pg = pg + 1
Next
End With
shTr.Activate
With ActiveSheet.PageSetup
.Zoom = False
.Orientation = xlPortrait
.FitToPagesWide = 1
.FitToPagesTall = False
.PrintArea = MainRng.Address
End With
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="c:\users\user\Desktop\test.pdf", _
Quality:=xlQualityStandard, _
IgnorePrintAreas:=False, _
IncludeDocProperties:=True, _
OpenAfterPublish:=True
End Sub
答案 1 :(得分:1)
由于某种原因,Ahmed AU的代码对我而言无法100%正常运行,因此我对其进行了一些更改。不用隐藏我不使用的行,而是取消隐藏我使用的行。
With shTransformed
.Cells.PageBreak = xlPageBreakNone
.Rows.EntireRow.Hidden = True
.VPageBreaks.Add shTransformed.Range("J1")
For Each Ar In Rng.Areas
.Range(Ar.Address).EntireRow.Hidden = False
.HPageBreaks.Add Ar(Ar.Rows.Count, Ar.Columns.Count).Offset(1, 0)
Next Ar
End With
Set MainRng = shTransformed.Range("A" & shTransformed.Cells(1, 1).End(xlDown).Row - 1 & ":I" & shTransformed.Cells(shTransformed.Rows.Count, 1).End(xlUp).Row)
'Export to PDF code here
答案 2 :(得分:0)
.PrintArea
需要一个字符串而不是一个范围。因此,现在,它仅从范围的第一个单元格B1中获取值。您需要自己连接这些值,并将连接后的字符串用作.PrintArea
的值。
https://docs.microsoft.com/en-us/office/vba/api/excel.pagesetup.printarea