意外的手动分页行为。为什么excel在错误的行上添加额外的分页符/中断?

问题描述 投票:0回答:1

我正在办公室自动创建一些文书工作。在一张纸上,我让用户输入需要有选择地复制到某些其他工作表的数据,这些工作表将打印出来供生产车间使用。目前我的“发货”表的格式有问题。第一张纸是空的。用户将使用此页面通过手写“准备”表格中的行项目中“容器类型和编号”下列出的每个唯一的包,包和托盘来总结包装清单,这些都在第2页( +)。

运行宏后,Excel在我手动设置分页符的每一行之后立即添加了一个额外的分页符。当我在“页面布局”视图中并切换到“发货”工作表时,对于我正在使用的当前示例数据,它看起来长达5页。但是,除了封面页之外,数据应该只需要2页,总共3页。页面2和4似乎只有一行,并在滚动时快速跳转到下一页。此外,我只是注意到我用于页面2+的标题仅出现在单行页面2和第3页上显示的“页面2”的其余部分上。第4页和第5页根本不显示标题使这个问题难以捕捉的原因是打印预览仅显示3个页面,并且在它们之前应该具有手动分页符的行被移位以显示为前一页的最后一行。

我尝试用三种略有不同的方式编写这个宏:1。Sheet.Rows(#)。PageBreak = xlPageBreakManual 2. Sheet.HPageBreaks.Add Before:= Sheet.Rows(#)3。Sheet.HPageBreaks(#)。Location = Sheet.Range(“A”&#)

注意:我在使用选项(3)遇到重复的“运行时错误'9':下标超出范围”错误后发现Microsoft的这篇文章,并相应地重新编码该选项https://support.microsoft.com/en-us/help/210663/you-receive-a-subscript-out-of-range-error-message-when-you-use-hpageb

最奇怪的部分是,如果我在调试模式下逐行进入选项(3),宏实际上正确格式化页面...

Here is the relevant code:

Option Explicit

'Public sSht As Worksheet
'Public sDatRng As Range, pDatRng As Range, pCopyRng As Range
'Public sCopyRow As Long, pCopyRow As Long
'Public sNumRows As Long, sHeadFootRows As Long, pNumRows As Long, pHeadFootRows As Long

Sub formatShipV1(numPgs As Long)
    Dim rng As Range
    Dim i As Long
    Dim currcell As Range

    Application.PrintCommunication = False

    With sSht
        .Cells.PageBreak = xlPageBreakNone

        With .PageSetup
            .Zoom = False
            .PaperSize = xlPaperLetter
            .Orientation = xlPortrait
            .PrintArea = sSht.Range("A1:J" & ((sNumRows + sHeadFootRows) + (numPgs * (pNumRows + pHeadFootRows)))).Address
            .LeftMargin = Application.InchesToPoints(0.2)
            .RightMargin = Application.InchesToPoints(0.2)
            .TopMargin = Application.InchesToPoints(0.6)
            .BottomMargin = Application.InchesToPoints(0.6)
            .HeaderMargin = Application.InchesToPoints(0.1)
            .FooterMargin = Application.InchesToPoints(0.1)
            .FitToPagesWide = 1
            .FitToPagesTall = numPgs + 1
            .CenterHorizontally = True
            .CenterVertically = False
        End With

        For i = 0 To (numPgs - 1)
            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 1).PageBreak = xlPageBreakManual

            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 1).RowHeight = Application.InchesToPoints(0.25)
            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 2).RowHeight = Application.InchesToPoints(0.3)
            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 3).RowHeight = Application.InchesToPoints(0.19)
            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 4).RowHeight = Application.InchesToPoints(0.57)

            Set rng = sSht.Range(Cells(((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 5), 1).Address & ":" & Cells(((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 26), 10).Address)
            rng.RowHeight = Application.InchesToPoints(0.38)

            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 27).RowHeight = Application.InchesToPoints(0.23)
        Next
    End With

    Application.PrintCommunication = True
End Sub

Sub formatShipV2(numPgs As Long)
    Dim rng As Range
    Dim i As Long
    Dim currcell As Range

    Application.PrintCommunication = False

    With sSht
        .ResetAllPageBreaks

        With .PageSetup
            .Zoom = False
            .PaperSize = xlPaperLetter
            .Orientation = xlPortrait
            .PrintArea = sSht.Range("A1:J" & ((sNumRows + sHeadFootRows) + (numPgs * (pNumRows + pHeadFootRows)))).Address
            .LeftMargin = Application.InchesToPoints(0.2)
            .RightMargin = Application.InchesToPoints(0.2)
            .TopMargin = Application.InchesToPoints(0.6)
            .BottomMargin = Application.InchesToPoints(0.6)
            .HeaderMargin = Application.InchesToPoints(0.1)
            .FooterMargin = Application.InchesToPoints(0.1)
            .FitToPagesWide = 1
            .FitToPagesTall = numPgs + 1
            .CenterHorizontally = True
            .CenterVertically = False
        End With

        For i = 0 To (numPgs - 1)
            .HPageBreaks.Add Before:=sSht.Rows(((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 1))

            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 1).RowHeight = Application.InchesToPoints(0.25)
            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 2).RowHeight = Application.InchesToPoints(0.3)
            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 3).RowHeight = Application.InchesToPoints(0.19)
            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 4).RowHeight = Application.InchesToPoints(0.57)

            Set rng = sSht.Range(Cells(((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 5), 1).Address & ":" & Cells(((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 26), 10).Address)
            rng.RowHeight = Application.InchesToPoints(0.38)

            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 27).RowHeight = Application.InchesToPoints(0.23)
        Next
    End With

    Application.PrintCommunication = True
End Sub

Sub formatShipV3(numPgs As Long)
    Dim rng As Range
    Dim i As Long
    Dim currcell As Range

    Call endOptimize
    Set currcell = ActiveCell
    Range("IV65536").Select

    Application.PrintCommunication = False

    With sSht
        .Activate
        ActiveWindow.View = xlPageBreakPreview
        .ResetAllPageBreaks

        With .PageSetup
            .Zoom = False
            .PaperSize = xlPaperLetter
            .Orientation = xlPortrait
            .PrintArea = sSht.Range("A1:J" & ((sNumRows + sHeadFootRows) + (numPgs * (pNumRows + pHeadFootRows)))).Address
            .LeftMargin = Application.InchesToPoints(0.2)
            .RightMargin = Application.InchesToPoints(0.2)
            .TopMargin = Application.InchesToPoints(0.6)
            .BottomMargin = Application.InchesToPoints(0.6)
            .HeaderMargin = Application.InchesToPoints(0.1)
            .FooterMargin = Application.InchesToPoints(0.1)
            .FitToPagesWide = 1
            .FitToPagesTall = numPgs + 1
            .CenterHorizontally = True
            .CenterVertically = False
        End With

        For i = 0 To (numPgs - 1)
            Set .HPageBreaks(i + 1).Location = sSht.Range("A" & ((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 1))
            DoEvents

            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 1).RowHeight = Application.InchesToPoints(0.25)
            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 2).RowHeight = Application.InchesToPoints(0.3)
            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 3).RowHeight = Application.InchesToPoints(0.19)
            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 4).RowHeight = Application.InchesToPoints(0.57)

            Set rng = sSht.Range(Cells(((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 5), 1).Address & ":" & Cells(((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 26), 10).Address)
            rng.RowHeight = Application.InchesToPoints(0.38)

            .Rows((sNumRows + sHeadFootRows) + (i * (pNumRows + pHeadFootRows)) + 27).RowHeight = Application.InchesToPoints(0.23)
        Next

        ActiveWindow.View = xlPageLayoutView

    End With

    Application.PrintCommunication = True

    sSht.Activate
    sSht.Range(currcell.Address).Select
    Call startOptimize
End Sub

Sub startOptimize()
    Application.ScreenUpdating = False
    Application.DisplayStatusBar = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
    ActiveSheet.DisplayPageBreaks = False
End Sub

Sub endOptimize()
    Application.ScreenUpdating = True
    Application.DisplayStatusBar = True
    Application.Calculation = xlCalculationAutomatic
    Application.EnableEvents = True
    ActiveSheet.DisplayPageBreaks = True
End Sub

'Sub runMacro()
'    Call startOptimize
'    ...
'    Dim sNumSht As Long
'    ...
'    Other variable declarations
'    ...
'    Set sSht = datBk.Worksheets("Ship")
'    Set sDatRng = sSht.Range("B6:J27")
'    Set pDatRng = sSht.Range("B32:J53")
'    Set pCopyRng = sSht.Range("A28:J54")
'    sNumRows = 22
'    sHeadFootRows = 5
'    pCopyRow = 55
'    pNumRows = 22
'    pHeadFootRows = 5
'    ...
'    Other variable initializations
'    ...
'    Code to calculate what data to copy to the "Ship" sheet, and how many pages "sNumSht" should equal
'    ...
'    Call formatShipV1((sNumSht + 1))
'    '-OR-
'    Call formatShipV2((sNumSht + 1))
'    '-OR-
'    Call formatShipV3((sNumSht + 1))
'    ...
'    Code to copy previously determined data to ship sheet
'    ...
'    Code to execute the rest of the macro
'    ...
'    Call endOptimize
'End Sub

“船舶”表格的预期格式[通过“踏入”选项(3)生成]:

“发货”表格的实际格式(正​​常运行时选项1-3):

- 在Excel中显示(仅显示第1-5页,共5页):

- 通过Excel打印预览打印为PDF:

excel vba page-break
1个回答
1
投票

附近许多休息的原因: 它们可能是在密集测试期间输入的。

错误背景:

  • 您尝试首先通过Worksheet.Cells.PageBreak = xlPageBreakNone重置所有分页符。 这没用,所以你所有的手动测试休息仍然存在。
  • 如果你定义PageSetup.Zoom = FalsePageSetup.FitToPagesTall = 3, 然后额外的手动分页符不会生效。 手动中断仍然可以设置,但既不有效也不可见。

解决方案1:如果......

  • 打印区域与页面宽度的匹配通常是可以的
  • 您的任何页面都必须具有比自动缩放更少的计数行
  • 并且您的页面都不得拥有比自动缩放更多的行

...然后按如下方式设置手动水平分页符:

  1. 首先按Worksheet.ResetAllPageBreaks重置所有分页符
  2. 然后定义分页符的自动部分 PageSetup.FitToPagesWide = 1 Pagesetup.FitToPagesTall = False 不要设置缩放。通过上面的行设置为False。
  3. 在每个页面的末尾放置一个手动水平分页符,其中包含的行数应少于自动设置的行数。从上到下做到这一点。

解决方案2:如果您的任何页面需要比上面更多的行,那么请执行以下操作:

  1. 首先按Worksheet.ResetAllPageBreaks重置所有分页符
  2. 定义适当的缩放级别,适合具有多行的页面,例如: G。 PageSetup.Zoom = 80 PageSetup.FitToPagesWide = False PageSetup.FitToPagesTall = False
  3. 放置手动水平分页符以根据需要缩短页面。从文档的开头到结尾执行此操作。

可以通过以下任一方法设置手动水平分页符:

  • Worksheet.HPageBreaks.Add Before:=ws.Rows(10)
  • Worksheet.Rows(10).PageBreak = xlPageBreakManual

第一种方法是faster而不是第二种方法。


Worksheet.HPageBreaks.Count将显示打印区域中的水平分页符数,包括自动中断。以下将不会“转换”第一个自动分页符。它只是移动第一个手动中断,如果至少有一个:

  • Set Worksheet.HPageBreaks(1).Location = ws.Rows(20)
© www.soinside.com 2019 - 2024. All rights reserved.