我有一个工作簿,其中包含多个定义了打印区域的工作表。每个打印区域定义了0个或更多分页符。如何仅仅选择第1页中的单元格或仅选择打印区域中第2页中的单元格。我已编写代码来选择第1页:
Dim wksht As Worksheet
Dim rng As Range
Dim prntArea As Range
Set wksht = ThisWorkbook.ActiveSheet
Set prntArea = wksht.Range(wksht.PageSetup.printArea)
'Select Page 1 of print area
'First cell of print area
Set rng = prntArea.Cells(1, 1)
'extend to one row above page break
Set rng = wksht.Range(rng.Address, wksht.HPageBreaks(1).Location.Offset(-1).Address)
'extend to last column of Print Area
Set rng = wksht.Range(rng.Address, wksht.Cells(prntArea.Cells(1, 1).Row, prntArea.Cells(prntArea.Count).Column).Address)
rng.Copy
然后我必须编写更多代码来选择第二页和第三页(如果它们存在),从一个分页符到下一个或打印区域的末尾。
是否有更简单的方法来选择打印区域的其中一个页面?
我想做什么:
用户可以使用ExportToFixedFormat打印到PDF,并使用打印区域。他们希望能够打印到PowerPoint和Word。因此,我正在创建一个新的PowerPoint演示文稿并选择打印页面区域,然后复制并粘贴到新幻灯片中。除了试图找到一种更简单的方法来选择打印页面之外,我已经完成了所有工作。
答案 0 :(得分:1)
您需要使用Worksheet.VPageBreaks
(垂直分页符)和Worksheet.HPageBreaks
(horiz分页符)集合属性。但是,我建议你使用类似于下面的方法,而不是假设printarea总是1页宽。)
您可以对它们进行分析,以确定每个页面所涵盖的区域。
以下代码显示了一些使用它们并与之交互的方法。看看他们的方法和属性,特别是Location属性。查看调试文本(确保您可以看到Immediates窗口),以查看.Locations如何与您在页面上看到的分页符对应。
Sub PageBreaksExample()
Dim ws As Worksheet
Dim hpb As HPageBreak
Dim vpb As VPageBreak
Set ws = ActiveSheet
For Each hpb In ws.HPageBreaks
Debug.Print "Horizontal pagebreak: " & hpb.Location.Address
Next hpb
For Each vpb In ws.VPageBreaks
Debug.Print "Vertical pagebreak:" & vpb.Location.Address
Next vpb
'You can get the first page range like: (adjust for second page etc)
Dim rng
Set rng = ws.Range( _
ws.HPageBreaks(1).Location.Offset(-1, 0), _
ws.VPageBreaks(1).Location.Offset(0, -1))
Debug.Print "Range of first page:" & rng.Address
End Sub
请注意,通过查看HPageBreaks(1)和VPageBreaks(1)始终可以找到第一页。但是,根据工作表设置为打印后续页面的方式(向下然后超过vs然后向下)会影响您应该如何找到第二页和后续页面。您可以通过查看Worksheet.PageSetup.Order
属性(xlDownThenOver
或xlOverThenDown
)来确定其运行方式。
答案 1 :(得分:0)
这最终是我在这个问题上提出来的。这仅适用于水平分页符。它分为3个部分。首先是没有分页符,只打印一页。第二个是有一个分页符和两个要打印的页面。然后第三部分是2个或更多分页符,这意味着有3页或更多页。如果您看到 *****在选定范围上工作,则表示rng包含打印区域的页面,这是您要在打印区域的页面上执行操作的位置。
Dim wksht as Worksheet
For Each wksht In ThisWorkbook.Worksheets
Dim prntArea As Range
Set prntArea = wksht.Range(wksht.PageSetup.printArea)
If wksht.HPageBreaks.Count = 0 Then 'No Page Breaks in Print Area
Set rng = prntArea
'*****Work on selected range
End If
If wksht.HPageBreaks.Count = 1 Then 'One Page Break = 2 Pages
' Page 1
Set rng = prntArea.Cells(1, 1)
Set rng = wksht.Range(rng.Address, wksht.HPageBreaks(1).Location.Offset(-1).Address)
Set rng = Application.Intersect(rng.EntireRow, prntArea)
'*****Work on Selected Range
' Page 2
Set rng = wksht.Range(wksht.HPageBreaks(1).Location.Address, prntArea.Cells(prntArea.Rows.Count, 1))
Set rng = Application.Intersect(rng.EntireRow, prntArea)
'*****Work on selected Range
End If
If wksht.HPageBreaks.Count > 1 Then '2 or more Page Breaks = 3 or more pages
' Page 1
Set rng = prntArea.Cells(1, 1)
Set rng = wksht.Range(rng.Address, wksht.HPageBreaks(1).Location.Offset(-1).Address)
Set rng = wksht.Range(rng.Address, wksht.Cells(prntArea.Cells(1, 1).Row, prntArea.Cells(prntArea.Count).Column).Address)
'*****Work on Selected Range
' Pages 2 through (pages - 1)
Dim pgBreaks As Integer
For pgBreaks = 1 To wksht.HPageBreaks.Count - 1
Set rng = wksht.Range(wksht.HPageBreaks(pgBreaks).Location.Address, wksht.HPageBreaks(pgBreaks + 1).Location.Offset(-1).Address)
If rng.Columns.Count < prntArea.Columns.Count Then
Set rng = rng.Resize(rng.Rows.Count, prntArea.Columns.Count)
End If
'*****Work on Selected Range
Next pgBreaks
'Last Page
Set rng = wksht.Range(wksht.HPageBreaks(wksht.HPageBreaks.Count).Location.Address, prntArea.Cells(prntArea.Rows.Count, 1))
Set rng = Application.Intersect(rng.EntireRow, prntArea)
'*****Work on Selected Range
End If
Next wksht
答案 2 :(得分:-2)
我需要基于分页后标题文本/页码作为页面导航链接的一张excel表格的内容表