意外的手动分页符行为。为什么excel在错误的行上添加额外的分页符/分页符?
我正在办公室自动化一些文书工作。在一张工作表中,我让用户输入需要选择性复制到某些其他工作表的数据,这些工作表将打印出来供生产车间使用目前我的“发货”表的格式有问题。第一张表是空的。预计用户将使用此页面通过手写第2(+)页“准备”表上的行项目中“集装箱类型和编号”下列出的每个独特的箱子、包裹和托盘来汇总装箱单 运行宏后,Excel似乎会在我手动设置分页符的每一行之后立即添加一个额外的分页符。当我在“页面布局”视图中并切换到“发货”工作表时,我使用的当前示例数据似乎有5页长。但是,除了封面之外,数据只需要2页,总共3页。第2页和第4页似乎只有一行,滚动时会快速跳转到下一页。此外,我刚刚注意到,我用于第2+页的页眉仅出现在单行第2页的上方,其余的“第2页”显示在第3页上。第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找到了这篇文章,并相应地重新编码了该选项 最奇怪的是,如果我在调试模式下逐行进入选项(3),宏实际上会正确格式化页面 以下是相关代码: “发货”表的预期格式[通过“进入”选项(3)生成]: “发货”表的实际格式(正常运行时选项1-3): -如Excel所示(仅显示第1-3页,共5页): -通过Excel的打印预览打印到PDF:意外的手动分页符行为。为什么excel在错误的行上添加额外的分页符/分页符?,excel,vba,page-break,Excel,Vba,Page Break,我正在办公室自动化一些文书工作。在一张工作表中,我让用户输入需要选择性复制到某些其他工作表的数据,这些工作表将打印出来供生产车间使用目前我的“发货”表的格式有问题。第一张表是空的。预计用户将使用此页面通过手写第2(+)页“准备”表上的行项目中“集装箱类型和编号”下列出的每个独特的箱子、包裹和托盘来汇总装箱单 运行宏后,Excel似乎会在我手动设置分页符的每一行之后立即添加一个额外的分页符。当我在“页面布局”视图中并切换到“发货”工作表时,我使用的当前示例数据似乎有5页长。但是,除了封面之外,数据
造成附近许多休息的原因:
它们可能是在密集测试期间输入的 错误背景:
- 您试图先通过
重置所有分页符Worksheet.Cells.PageBreak=xlPageBreakNone
这不起作用,所以你所有的手动测试中断仍然存在 - 如果定义了
和PageSetup.Zoom=False
,PageSetup.FitToPagesTall=3
然后,额外的手动分页符不会生效。
仍然可以设置手动中断,但既不有效也不可见。
解决方案1:如果
- 打印区域与页面宽度的匹配通常是可以的
- 任何页面的行数都必须少于自动缩放
- 您的页面中没有一个需要比自动缩放更多的行
PageSetup.FitToPagesWide=1
Pagesetup.FitToPagesTall=False
不要设置缩放。通过上述行将其设置为False
解决方案2:如果您的任何页面需要比上面更多的行,请按如下方式操作:
PageSetup.Zoom=80
PageSetup.FitToPagesWide=False
PageSetup.FitToPagesTall=False
设置手动水平分页符可以通过以下任一方法完成:
Worksheet.HPageBreaks.addbefore:=ws.Rows(10)
工作表.行(10).PageBreak=xlPageBreakManual
工作表.HPageBreaks.Count
将显示打印区域中水平分页符的数量,包括自动分页符。以下内容不会“转换”第一个自动分页符。如果至少有一个手动中断,则只移动第一个手动中断:
设置工作表.HPageBreaks(1)。位置=ws.Rows(20)
sSht.HPageBreaks(1)开始的每个分页符的索引。Location=sSht.Range(“A28”)
谢谢您的建议。我将尝试修改所有变体以使用工作表。ResetAllPageBreaks
。对于您关于设置打印比例的观点,您的意思是我应该在添加手动分页符之前设置。FitToPagesTall=Automatic
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