» Add page breaks to sorted data using VBA in Microsoft Excel
The tip submitted by Kevin from Sheffield UK
Sort the data before operate the macro.
Sub Pagebreak()
ActiveSheet.ResetAllPageBreaks 'Clear existing page breaks
Dim Rng As Range 'number of rows
Dim lngCOL As Long 'column number to use - user input
Dim lngROW As Long 'row count
On Error GoTo EndMacro
Application.ScreenUpdating = False
lngCOL = InputBox("Enter the column NUMBER to use", "Column page break")
Set Rng = ActiveSheet.UsedRange.Rows
For lngROW = 3 To Rng.Rows.Count 'start by comparing row 2 and 3
If Cells(lngROW, lngCOL).Formula <> Cells(lngROW - 1, lngCOL).Formula Then
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Cells(lngROW, lngCOL)
Application.StatusBar = "Row: " + Format(lngROW)
End If
Next lngROW
Application.StatusBar = "Done"
EndMacro:
Set Rng = Nothing
Application.ScreenUpdating = True
End Sub
Book Store:
Recommended Books:
- Business Analysis with Microsoft Excel (2nd Edition)
- PowerPoint® 2002 For Dummies®
- The Complete Book of Business Plans: Simple Steps to Writing a Powerful Business Plan (Small Business Sourcebooks)
- The Fall of Advertising and the Rise of PR
- MP Managerial Accounting w/ Topic Tackler, Net Tutor, & PowerWeb
- Adventure Capitalist: The Ultimate Road Trip

