Excel VBA Macro: Insert Page Break When Row Contains Specific Text
Question details
The user needs an Excel VBA macro that automatically adds a manual page break above any row containing the text 'Start Date'.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Formatting a large report or worksheet where specific sections marked by a 'Start Date' need to print on separate pages.
- Observed behavior
- The user wants to automate the insertion of page breaks but needs functional VBA code that correctly identifies the text without scanning entire columns and causing the application to freeze.
Before running the macro, ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and identify the specific column containing the 'Start Date' text to avoid scanning the entire worksheet.
Use a Targeted VBA Macro to Insert Page Breaks
Run a targeted VBA script to scan a specific data range and insert page breaks wherever the specified text is found.
To avoid crashing or freezing Excel, it is crucial to restrict the macro to a specific range (e.g., A1:A200) or dynamically calculate the last used row, rather than processing an entire million-row column.
Press Alt + F11 in your Excel workbook to open the Microsoft Visual Basic for Applications window.
Click 'Insert' from the top menu, then select 'Module' to create a new blank script window.
Copy and paste the following code: Sub BreakItUp() Dim rng As Range, rCell As Range Set rng = Worksheets("Sheet4").Range("A1:A20") For Each rCell In rng If LCase(rCell.Value) = "start date" Then rCell.EntireRow.PageBreak = xlPageBreakManual End If Next rCell End Sub
Change 'Sheet4' and 'A1:A20' to match your actual worksheet name and data range. Close the editor and press Alt + F8, select 'BreakItUp', and click 'Run'.

Automate Page Breaks with WPS Spreadsheet Macros
WPS Office fully supports VBA macros, allowing you to run scripts for formatting and automating page breaks with high compatibility and smooth performance.
- 1. Open your file in WPS: Launch WPS Spreadsheet and open your existing report or workbook.
- 2. Access the Developer Tools: Go to the 'Developer' tab on the ribbon and click 'Macros', or press Alt + F11 to open the VBA Editor.
- 3. Insert the Script: Add a new module and paste the page break VBA code provided in the solution.
- 4. Execute the Macro: Press F5 or click the Run button to instantly format your report with the necessary page breaks.

Frequently Asked Questions
Why does my Excel macro freeze when searching for the text?
Scanning an entire column (like A:A) forces the macro to check over 1 million rows, which causes performance issues. Always restrict the range to your actual data bounds (e.g., A1:A500) or use a dynamic variable to find the last used row.
How do I remove all manual page breaks using VBA?
You can clear all manual page breaks across an entire worksheet by running a simple one-line macro command: ActiveSheet.ResetAllPageBreaks.
Can I use a different phrase instead of 'Start Date'?
Yes, simply modify the text string inside the quotation marks in the VBA code from 'start date' to your desired text. Because the code uses the LCase() function, make sure your target text string in the code is typed in all lowercase letters.




