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.

Conditional Page Breaks

Written by Allen Wyatt (last updated October 8, 2022)
This tip applies to Excel 97, 2000, 2002, and 2003


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:

  1. Make sure your table contains column labels. For instance, if column A contains the department names, then cell A1 could contain a label such as "Department." Make sure all the columns have labels.
  2. Sort the data in your table, using the department column as the key.
  3. With any cell within the table still selected, choose Subtotals from the Data menu. Excel displays the Subtotals dialog box.
  4. Using the At Each Change In drop-down list, select Department.
  5. Using the Use Function drop-down list, select Count.
  6. Using the Add Subtotal To list, select the name of the column where you want your subtotal to appear.
  7. Make sure the Page Break Between Groups check box is selected.
  8. Click on OK. Excel adds the subtotals and the page counts, as directed.

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:

http://support.microsoft.com/kb/284916

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."

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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.

Author Bio

Allen Wyatt

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. ...

MORE FROM ALLEN

Understanding Document Variables

When working with macros, you may want to create a variable that will remain constant from one instance of the macro to ...

Discover More

Hiding and Unhiding Columns

Want to hide a column so it doesn't appear in the worksheet? It's easy to do using the formatting capabilities of Excel.

Discover More

Converting Text Into a Table

Need a block of text to appear in a table? Word can do the conversion for you very quickly.

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (menu)

Using Fractional Number Formats

If you want information to display on the screen using fractions instead of decimals, you're in luck. Excel provides ...

Discover More

Altering the Displayed Format of Numbers to the Nearest 100

Want information in a worksheet to be formatted and displayed as rounded to a power of ten? You may be out of luck, ...

Discover More

Displaying Zeros

There are times when displaying zero values in a worksheet (especially if there are lots of them) can be distracting from ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 + 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.