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:
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:
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 MoreWhen you create workbooks for others to use, you might want to make sure that they can't change the formatting and paper ...
Discover MoreNeed 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 MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-02-20 09:09:26
What is wrong with the below code? I have ZERO knowledge of VBA. Help a moron, please.
I want to fix page breaks on a pivot table, based on the value of a cell.
I have created a column that will set itself to PageBreak if it meets certain conditions and am trying to test this column as follows.
Sub PageBreak()
Dim CellRange As Range
Dim TestCell As Range
ActiveSheet.ResetAllPageBreaks
Set CellRange = A1.A475
For Each TestCell In CellRange
If TestCell.Value = "PageBreak" Then
ActiveSheet.Rows(TestCell.Row).PageBreak = xlPageBreakManual
End If
Next TestCell
End Sub
2019-07-22 08:49:05
Chris Mcdonnell
How can you adjust this so that it only inserts page breaks for visible rows, and ignores hidden rows? I have filtered data that is causing page breaks to be entered off the hidden rows, but would like the macro to ignore those hidden rows.
Thanks!
2015-12-17 12:55:03
Grigore Florin
A most useful tip. It brought me a lot of fame :)))
Thank you tips.net
2015-05-19 19:11:13
ONE OF THE MOST USEFUL MACROS I HAVE SEEN. WILL SAVE HOURS. WORKS GREAT.
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments