是否可以在Excel 2007中的VBA中生成自动分页符,忽略分割单元格(合并相邻单元格后)?
例如,在下面的示例中,我希望分页符在第51行上方,而不是在合并单元格A51-A53的中间切换。
我尝试的代码是:
workSheet.Range("A2:A" & bottomRow).PageBreak = xlPageBreakAutomatic
通过仅包括范围中的列A,我希望它不会生成切断此列中单元格的分页符。
答案 0 :(得分:2)
试试这段代码。
Sub HPageBreaks_and_MergeCells()
Dim sh As Worksheet
Dim NextPageBreakNumber As Long
Dim PageBreakFirstLine As Object
Dim LineNumber As Long
Set sh = ThisWorkbook.ActiveSheet
ActiveWindow.View = xlPageBreakPreview
sh.ResetAllPageBreaks
NextPageBreakNumber = 1
While NextPageBreakNumber <= sh.HPageBreaks.Count
Set PageBreakFirstLine = sh.HPageBreaks(NextPageBreakNumber).Location
LineNumber = PageBreakFirstLine.Row
If sh.Cells(LineNumber, 1).MergeCells = True Then
Set sh.HPageBreaks(NextPageBreakNumber).Location = sh.Cells(sh.Cells(LineNumber, 1).MergeArea.Row, 1)
End If
NextPageBreakNumber = NextPageBreakNumber + 1
Wend
ActiveWindow.View = xlNormalView
End Sub