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.
With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company.
Learn more about Allen...
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.
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.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!