【问题标题】:Inserting Page Break With VBA使用 VBA 插入分页符
【发布时间】:2021-10-23 16:06:05
【问题描述】:

我正在尝试插入分页符,以便每 8 行打印 8 行 Excel 工作表。我遇到的问题是,当使用“ActiveWindow.View = xlPageBreakPreview”方法时,第一个分页符发生在第 10 行之后,然后从那里开始,每 8 行发生一次。这是我目前使用的代码:

ActiveSheet.ResetAllPageBreaks
ActiveWindow.View = xlPageBreakPreview
ActiveSheet.VPageBreaks(2).DragOff Direction:=xlToRight, RegionIndex:=1
ActiveSheet.VPageBreaks(1).DragOff Direction:=xlToRight, RegionIndex:=1

With ActiveSheet.PageSetup
    .CenterHeader = "&""Chiller""&75WOC"
    .CenterFooter = "&""Chiller""&75 PLACARD"
    .PrintQuality = 600
    .CenterHorizontally = True
    .CenterVertically = True
    .Orientation = xlLandscape
    .PaperSize = xlPaperLetter
    .Zoom = 45
    .LeftMargin = Application.InchesToPoints(0)
    .RightMargin = Application.InchesToPoints(0)
    .TopMargin = Application.InchesToPoints(0.75)
    .BottomMargin = Application.InchesToPoints(0.75)
    .HeaderMargin = Application.InchesToPoints(0.3)
    .FooterMargin = Application.InchesToPoints(0.3)
End With

我尝试添加此循环以手动添加分页符,但它使代码陷入困境,所以我想知道是否有一个我不知道的更简单的解决方案。

非常感谢任何帮助。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    您可以像这样添加分页符:

    Worksheets("Sheet1").HPageBreaks.Add Before:=Worksheets("Sheet1").Rows(25)
    Worksheets("Sheet1").VPageBreaks.Add Before:=Worksheets("Sheet1").Columns("J")
    
    Worksheets("Sheet1").Rows(25).PageBreak = xlPageBreakManual
    Worksheets("Sheet1").Columns("J").PageBreak = xlPageBreakManual
    

    要在第 10 行之后获得每 8ᵗʰ 行,请使用类似循环

    Dim iRow As Long
    For iRow = 10 To LastRow Step 8
        'your page break code here
    Next iRow
    

    其中LastRow 是最后使用的行,例如喜欢

    Dim LastRow As Long
    LastRow = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row 'find last used row in column A
    

    所以可能的结果是这样的:

    Option Explicit
    
    Public Sub AddPageBreaks()
        Dim ws As Worksheet
        Set ws = ThisWorkbook.Worksheets("Sheet1") 
    
        Dim LastRow As Long
        LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'find last used row in column A
    
        Dim iRow As Long
        For iRow = 10 To LastRow Step 8
            ws.HPageBreaks.Add Before:=ws.Rows(iRow)
            ws.Rows(iRow).PageBreak = xlPageBreakManual
        Next iRow
    End Sub
    

    【讨论】:

    • 非常感谢! iRow for 循环比我使用的循环更有效。
    【解决方案2】:

    这太棒了 - 谢谢!

    对于那些可能对创建 VERTICAL 分页符感兴趣的人 - 例如对于现金流中的每一年 - 我分享我的代码如下:

    Sub pagebreaks()
    'insert the required page breaks automatically
    ' see https://stackoverflow.com/questions/51632995/inserting-page-break-with-vba
    
        Dim ws As Worksheet
        Dim overallendcol As Long
        Dim mycol As Long
        
    'set the working sheet
        Set ws = Sheets("cf mthly")
    
    'clear existing page breaks
        ActiveWindow.View = xlNormalView
        ActiveSheet.Cells.pagebreak = xlPageBreakNone
    
    'establish the first new year column
        Cells(1, 1).Select
        Cells.Find(What:="Jan", After:=ActiveCell, LookIn:= _
            xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False).Activate
        newyearcol = ActiveCell.Column
    
    'establish the overall last column
        Cells(1, 1).Select
        Cells.Find(What:="totals", After:=ActiveCell, LookIn:= _
            xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:= _
            xlNext, MatchCase:=False, SearchFormat:=False).Activate
        overallendcol = ActiveCell.Column
    
    'establish each new year column
        Cells(1, 1).Select
        For mycol = newyearcol To overallendcol - 1 Step 12
            ws.Columns(newyearcol).pagebreak = xlPageBreakManual
        Next mycol
    
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-08
      相关资源
      最近更新 更多