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

Renaming a Workbook

Renaming a workbook from within Excel can seem daunting, but it is actually quite easy. All you need to do is use the ...

Discover More

Quickly Transposing Cells

If you want to turn a range of cells by 90 degrees within a worksheet, you need to understand how Excel can handle the ...

Discover More

Capitalizing the Word "I"

The first-person, singular pronoun "I" should always be capitalized, unless you are exercising poetic license. Word may ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

Working with Fonts

Windows allows you to install different fonts that control how information is displayed and printed. This tip gives a ...

Discover More

Setting Text Attributes

Want to change the attributes of your text (or what Excel refers to as font styles)? Here's how to do it.

Discover More

Understanding Color and Conditional Formatting Codes

When you create custom cell formats, you can include codes that allow you to set the color of a cell and that specify the ...

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