» 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:
- Retire Young, Retire Rich
- Positioning: The Battle for Your Mind
- Managing by the Numbers: A Commonsense Guide to Understanding and Using Your Company's Financials: An Essential Resource for Growing Businesses
- Managerial Accounting: Tools for Business Decision Making, WebCT, 2nd Edition
- 422 Tax Deductions for Businesses and Self-Employed Individuals : You Get a Raise Every Time You Find a Legitimate Tax Deduction
- The Fall of Advertising and the Rise of PR


