Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Conditional Page Breaks.
by Allen Wyatt
(last updated March 7, 2019)
Excel is a handy tool for keeping track of all sorts of data. Many people use it at work to create ad-hoc reports for different departments or projects. As you work with your data, you may wonder how you can automatically insert page breaks when the contents of a certain column change. For instance, you might have a column that contains department names, and you may want each department to start on a new page.
This is rather easy to do with the built-in Subtotals feature of Excel. All you need to do is follow these steps:
If, for some reason, you don't want to use the Subtotals feature, you can always write a macro that will remove all the page breaks in your worksheet, then add new page breaks at the appropriate places. The following macro will do the trick:
Sub PageBreak() Dim CellRange As Range Dim TestCell As Range ActiveSheet.ResetAllPageBreaks Set CellRange = Selection For Each TestCell In CellRange If TestCell.Value <> TestCell.Offset(-1, 0).Value Then ActiveSheet.Rows(TestCell.Row).PageBreak = xlPageBreakManual End If Next TestCell End Sub
To use the macro, simply select the cells you want to use as your key for doing the splits, minus the top cell. For instance, if the departments are in column A, rows 2 through 37, you would select the range in A3 through A37. Run the macro, and any old page breaks are removed and new ones added.
You should realize that Excel does have a limit when it comes to the number of manual page breaks that can be used in a worksheet. According to the Knowledge Base, that limit is around 1024 breaks. (The limit can vary slightly based on the version of Excel you are using, but is right around that point.) Here's the obscure write-up about the limit:
The upshot of this limit is that if you have quite a few page breaks to insert, the macro will crash when the page-break limit is exceded on the worksheet. The error will say something along the lines of "Unable to set the PageBreak property."
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2792) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Conditional Page Breaks.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Custom formats are great for defining how a specific value in a cell should look. They aren't that great at doing complex ...Discover More
When you create workbooks for others to use, you might want to make sure that they can't change the formatting and paper ...Discover More
Need to hide a given column based on the value in a particular cell? The easiest way to accomplish the task is to use a ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.