如果水平动态,如何在每个页面的顶部显示 table header
How to have the table header on the top of each page if horizontally dynamic
我的 table 的类别都在 A 列中,从 "A3" 开始。以下各列包含数据,每次报告 运行 时列数可能会有所不同。第 1 行有图表标题,第 2 行有图例 ("A1:G2")。 "H1" 结尾是空白。由于数据是动态的,因此页数也会变化。我想在每一页的顶部都有标题和图例。
如果我在页面设置中列出行,整行都会被选中。我需要重复的信息仅在("A1:G2")中。我无法编写代码来复制和粘贴 "A1:G2",因为我永远不知道我会有多少页。工作簿标题在所有页面上都列为 header。
Public Sub testsub()
Dim ws As Worksheet
Dim surf As Worksheet
With surf.PageSetup
.PrintTitleRows = ":"
.PrintTitleColumns = "$A:$A"
End With
Application.PrintCommunication = True
surf.PageSetup.PrintArea = ""
With surf.PageSetup
.LeftHeader = ""
.CenterHeader = "Test Workbook"
.RightHeader = ""
.LeftFooter = "&D"
.CenterFooter = "&G"
.RightFooter = "&P"
.CenterHorizontally = True
.CenterVertically = True
End With
Application.PrintCommunication = True
End Sub
我想在工作表的每一页上显示 "A1:G2"。谢谢!
此解决方案使用工作簿事件Workbook_BeforePrint
将以下过程复制到工作簿的 ThisWorkbook
对象模块中:
Option Explicit
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Call Print_Header_Update
End Sub
Sub Print_Header_Update()
Dim ws As Worksheet, vpb As VPageBreak, rHdr As Range, rg As Range
Set ws = ThisWorkbook.Worksheets("DATA") 'Update as required
With ws
Set rHdr = .Range("B1:G2") 'Update as required
Set rg = rHdr.Columns(8).Resize(2, -8 + .Columns.Count) 'Update as required
rg.ClearContents
For Each vpb In ws.VPageBreaks
rHdr.Copy
vpb.Location.Cells(1).PasteSpecial
Application.CutCopyMode = False
Selection.EntireColumn.AutoFit 'This might require fine-tuning
Next: End With
End Sub
有关详细信息,请参阅:
Workbook.BeforePrint event (Excel),
Worksheet.VPageBreaks property (Excel),
Range.Resize property (Excel),
Range.AutoFit method (Excel)
我的 table 的类别都在 A 列中,从 "A3" 开始。以下各列包含数据,每次报告 运行 时列数可能会有所不同。第 1 行有图表标题,第 2 行有图例 ("A1:G2")。 "H1" 结尾是空白。由于数据是动态的,因此页数也会变化。我想在每一页的顶部都有标题和图例。
如果我在页面设置中列出行,整行都会被选中。我需要重复的信息仅在("A1:G2")中。我无法编写代码来复制和粘贴 "A1:G2",因为我永远不知道我会有多少页。工作簿标题在所有页面上都列为 header。
Public Sub testsub()
Dim ws As Worksheet
Dim surf As Worksheet
With surf.PageSetup
.PrintTitleRows = ":"
.PrintTitleColumns = "$A:$A"
End With
Application.PrintCommunication = True
surf.PageSetup.PrintArea = ""
With surf.PageSetup
.LeftHeader = ""
.CenterHeader = "Test Workbook"
.RightHeader = ""
.LeftFooter = "&D"
.CenterFooter = "&G"
.RightFooter = "&P"
.CenterHorizontally = True
.CenterVertically = True
End With
Application.PrintCommunication = True
End Sub
我想在工作表的每一页上显示 "A1:G2"。谢谢!
此解决方案使用工作簿事件Workbook_BeforePrint
将以下过程复制到工作簿的 ThisWorkbook
对象模块中:
Option Explicit
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Call Print_Header_Update
End Sub
Sub Print_Header_Update()
Dim ws As Worksheet, vpb As VPageBreak, rHdr As Range, rg As Range
Set ws = ThisWorkbook.Worksheets("DATA") 'Update as required
With ws
Set rHdr = .Range("B1:G2") 'Update as required
Set rg = rHdr.Columns(8).Resize(2, -8 + .Columns.Count) 'Update as required
rg.ClearContents
For Each vpb In ws.VPageBreaks
rHdr.Copy
vpb.Location.Cells(1).PasteSpecial
Application.CutCopyMode = False
Selection.EntireColumn.AutoFit 'This might require fine-tuning
Next: End With
End Sub
有关详细信息,请参阅:
Workbook.BeforePrint event (Excel),
Worksheet.VPageBreaks property (Excel),
Range.Resize property (Excel),
Range.AutoFit method (Excel)