How to Insert Page Breaks in Excel When First Letter Changes via VBA Macro
Question details
The user needs an Excel VBA macro that can automatically insert a horizontal page break whenever the first character in a specific column changes, making it easier to separate categorized data for printing.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Organizing an alphabetically sorted list in Excel so that each new starting letter automatically prints on a new page without requiring manual page break insertion.
- Observed behavior
- Manual insertion of page breaks is tedious and prone to errors. A bottom-up scanning VBA macro is required to scan the column and automatically place a break before a new initial letter appears.
Ensure your data is properly sorted alphabetically by the target column before running the macro, and remember to save your workbook as a Macro-Enabled Workbook (.xlsm) to preserve your VBA code.
Create a Bottom-to-Top VBA Macro to Insert Page Breaks
Use a VBA script that loops through rows from the bottom up, comparing the first letter of each cell to the one above it to insert page breaks accurately.
Scanning from bottom to top prevents the row index from shifting as new page breaks are added. This solution also resets all existing page breaks first to prevent overlapping breaks from previous runs.
Open your Excel workbook and press ALT + F11 on your keyboard to open the Microsoft Visual Basic for Applications window.
Click 'Insert' from the top menu, then select 'Module' to create a blank window for your code.
Paste the following script into the module: Sub InsertPageBreaks() Dim r As Long, m As Long Application.ScreenUpdating = False ActiveSheet.ResetAllPageBreaks m = Range("A" & Rows.Count).End(xlUp).Row For r = m To 4 Step -1 If Left(Range("A" & r).Value, 1) <> Left(Range("A" & r - 1).Value, 1) Then ActiveSheet.HPageBreaks.Add Before:=Range("A" & r) End If Next r Application.ScreenUpdating = True End Sub
Close the VBA editor to return to your worksheet. Press ALT + F8, select 'InsertPageBreaks', and click 'Run'.

Modify the Macro to Group Specific Letters
Adjust the macro logic to conditionally skip page breaks for specific letters, grouping them together on a single page.
Run VBA Macros Seamlessly in WPS Office
WPS Spreadsheet comes with highly compatible, built-in VBA support. You can seamlessly run, edit, and create your Excel VBA macros to automate complex tasks like inserting page breaks without altering your code syntax.
- 1. Open your File in WPS Office: Download and install WPS Office, then open your Excel workbook (.xlsm or .xlsx) containing the target data in WPS Spreadsheet.
- 2. Access the Macro Editor: Navigate to the 'Tools' tab on the top ribbon and click 'Macro' or 'Visual Basic Editor' to open the VBA environment.
- 3. Insert the Code: Insert a new module and paste your bottom-up page break VBA script directly into the code editor.
- 4. Run the Macro: Run the macro to automatically apply the horizontal page breaks based on the changing starting letters in your column.

Frequently Asked Questions
Why do we need to scan the rows from bottom to top in the VBA macro?
When you add a page break or delete rows from top to bottom, the row indices shift downward, which can cause the loop to skip rows or process them incorrectly. Scanning backwards (from bottom to top using `Step -1`) ensures that adding page breaks below the current scanning point does not disrupt the loop's sequence.
How can I remove all existing page breaks before running the new macro?
You can include the command `ActiveSheet.ResetAllPageBreaks` at the beginning of your VBA macro. This automatically clears any manual or previously generated horizontal and vertical page breaks before the script applies the new ones.
What function extracts the first letter of a cell in VBA?
The `Left()` function is used to extract characters from the beginning of a string. For example, using `Left(Range("A" & r).Value, 1)` retrieves the first character of the cell in column A for row `r`, allowing you to compare it directly to the first character of the cell above it.




