【问题标题】:Trouble Removing extra page Break删除多余的分页符时遇到问题
【发布时间】:2017-10-12 16:08:11
【问题描述】:

我有一些代码可以在临时工作表上以可打印格式创建报告。在该代码中,它调用Sub SetPageBreaks(代码下方)。我无法删除在列 G:H 之间设置的特定分页符。

Sub SetPageBreaks(SheetObj As Worksheet)

    Dim TotRow%, LinesPerPg%, NumPgs%, Page%

    TotRow = SheetObj.UsedRange.Rows.Count
    LinesPerPg = 56
    NumPgs = RoundUp(TotRow / LinesPerPg)

    With SheetObj
        .ResetAllPageBreaks
        For Page = 1 To NumPgs
            .Rows(LinesPerPg * Page + 1).PageBreak = xlPageBreakManual
        Next Page
        .Columns("J").PageBreak = xlPageBreakManual
        .Columns("H").PageBreak = xlPageBreakNone
    End With

End Sub

Function RoundUp(ByVal Number As Double) As Long
    RoundUp = Application.WorksheetFunction.RoundUp(Number, 0)
End Function

列之间I:J 是页面的最后一列(它需要适合页面的整个宽度,可以预期一些缩放)。为什么我的代码没有删除 col H 上的中断?

编辑(新编码尝试):

我已将我的代码更新为以下内容,但没有成功(根据我的偏好调整边距,然后添加 .FitToPagesWide,它正在剪切我的最后一列。

Option Explicit

Sub SetPageBreaks(SheetObj As Worksheet)

    Dim TotRow%, LinesPerPg%, NumPgs%, Page%

    TotRow = SheetObj.UsedRange.Rows.Count
    LinesPerPg = 56
    NumPgs = RoundUp(TotRow / LinesPerPg)

    With SheetObj
        .ResetAllPageBreaks
        For Page = 1 To NumPgs
            .Rows(LinesPerPg * Page + 1).PageBreak = xlPageBreakManual
        Next Page
        With .PageSetup
            .HeaderMargin = 21.6
            .FooterMargin = 21.6
            .TopMargin = 86.4
            .LeftMargin = 18
            .RightMargin = 18
            .BottomMargin = 54
            .FitToPagesWide = 1
            .FitToPagesTall = False
        End With
    End With

End Sub

【问题讨论】:

    标签: excel page-break vba


    【解决方案1】:

    要进行某些缩放,您必须让 Excel 知道如何进行。在 With 块的开头,添加:

    .PageSetup.Zoom = False
    .PageSetup.PrintArea = "$A:$I"
    .PageSetup.FitToPagesWide = 1
    .PageSetup.FitToPagesTall = False
    

    您的最后 2 个带块行不再需要了。

    【讨论】:

    • 还要在.FitToPagesWide前设置PrintArea: .PrintArea = "$A:$I"
    • 我添加了.PageSetup.PrintArea = SheetObj.UsedRange.Address,结果相同(它在任何其他PageSetup 之前完成)
    • 我想通了。我需要添加.PageSetup.Zoom = False,因为一旦设置了.FitToPagesWide controls how the worksheet is scaled。谢谢你把我送到正确的方向,所以你的回答确实解决了我的问题。
    • 相应地编辑了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多