VBA & Macro Problems

Insert Excel VBA Page Breaks Based on Row Height and Section Markers

Rana GarciaRana Garcia Nov 20, 2026 870 views

Question details

Create an Excel VBA macro that places horizontal page breaks based on printable row height and qualifying section markers.

Insert Excel VBA Page Breaks Based on Row Height and Section Markers
Product
Microsoft Excel
Device & OS
not provided
Scenario
A worksheet contains section markers, such as values in column Q or K, that should guide page breaks when a page is near capacity.
Observed behavior
The desired result is a page break near a qualifying marker when the printable page is full, rather than a break after every matching row.
Before you start

In Excel, set the paper size, orientation, margins, print area, and any repeating print titles under Page Layout. Determine the usable height for worksheet rows in points at 100% scale; subtract space for margins, headers, footers, and repeating rows.

Solution 1Recommended

Use VBA to break pages near qualifying section markers

Use this macro to count visible row heights and prefer the last matching marker that fits on each page.

The macro places a break after the last qualifying marker that fits within the supplied page capacity. If no marker fits, it breaks after the last fitting row instead. A marker beyond the capacity is not chosen because Excel could insert an automatic break before it.

Replace 650 with your usable page height in points. The macro sets scaling to 100% and removes existing manual page breaks on the active sheet. Preview the result because Excel's actual pagination can vary with print settings.

1
Select the print rows

On the worksheet, select a contiguous range spanning the first through the last row to paginate. The macro uses the selection's row boundaries and checks column Q for the value 123.

2
Open the VBA editor

Press Alt+F11, select Insert > Module, and paste the following code into the module: Sub BreakPagesAtMarkers() Const PageCapacity As Double = 650 ' Usable row height in points Const MarkerColumn As String = "Q" Const MarkerValue As String = "123" Dim ws As Worksheet, area As Range Dim firstRow As Long, lastRow As Long, startRow As Long Dim r As Long, lastFit As Long, lastMarker As Long, breakAfter As Long Dim usedHeight As Double, rowHeight As Double, v As Variant Set ws = ActiveSheet If TypeName(Selection) <> "Range" Then Exit Sub Set area = Selection If area.Areas.Count <> 1 Then MsgBox "Select one contiguous range." Exit Sub End If firstRow = area.Row lastRow = area.Row + area.Rows.Count - 1 ws.PageSetup.Zoom = 100 ws.ResetAllPageBreaks startRow = firstRow Do While startRow <= lastRow usedHeight = 0 lastFit = 0 lastMarker = 0 For r = startRow To lastRow rowHeight = 0 If Not ws.Rows(r).Hidden Then rowHeight = ws.Rows(r).RowHeight If usedHeight + rowHeight > PageCapacity Then Exit For usedHeight = usedHeight + rowHeight lastFit = r v = ws.Cells(r, MarkerColumn).Value2 If Not IsError(v) And Not ws.Rows(r).Hidden Then If CStr(v) = MarkerValue Then lastMarker = r End If Next r If r > lastRow Then Exit Do If lastFit = 0 Then breakAfter = startRow ElseIf lastMarker > 0 Then breakAfter = lastMarker Else breakAfter = lastFit End If If breakAfter >= lastRow Then Exit Do ws.HPageBreaks.Add Before:=ws.Rows(breakAfter + 1) startRow = breakAfter + 1 Loop End Sub

3
Adjust the marker and capacity

In the module, change PageCapacity to the usable height in points. To use zero in column K instead, set MarkerColumn to "K" and MarkerValue to "0".

4
Run and check pagination

Return to Excel, select the intended range, and run BreakPagesAtMarkers from Developer > Macros. Check File > Print or View > Page Break Preview, then adjust PageCapacity or the print settings if needed.

Use VBA to break pages near qualifying section markers
Automatic breaks can still occur: Excel controls final pagination. A row taller than the supplied capacity, an inaccurate capacity, or different print settings can cause automatic breaks in addition to the manual breaks.
Free Microsoft Office alternative

Review Excel Worksheets in WPS Spreadsheet

WPS Office is a free, lightweight Office alternative with a familiar interface for opening and reviewing Excel worksheets. Its format compatibility makes it easy to move files between Office suites, but it does not fix or guarantee execution of this Excel VBA macro.

Open and review worksheets containing page-oriented reportsCompatible with Microsoft Excel XLS and XLSX formatsFamiliar spreadsheet interface for an easier transition from Microsoft OfficeFree, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does the macro not break after every marker?

It accumulates visible row heights until the next row would exceed PageCapacity, then chooses the last qualifying marker that fits. This avoids creating a new page for every matching value.

How do I use column K and a value of zero as the marker?

In the VBA module, set MarkerColumn to "K" and MarkerValue to "0", then run the macro again from Developer > Macros.

What happens if no marker fits on a page?

The macro inserts a break after the last row that fits within the supplied capacity. If even the first row is too tall, Excel may also insert an automatic break.

Why do the breaks look different in Print Preview?

The supplied capacity is an estimate of usable row height. Check paper size, margins, headers, footers, repeating rows, and 100% scaling in Page Layout and File > Print, then adjust PageCapacity.

Does the macro keep existing manual page breaks?

No. It calls ResetAllPageBreaks on the active sheet before adding the new breaks, so existing manual page breaks on that sheet are removed.