Page Break code Adjustment needed

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hi ,
Instead of hardcoding the number of rows to set the pagebreak into the script, I want to set that dynamically using some criteria:
In column B, I have say x, y, z, etc. they are sorted so all x before all y then all z etc. so I want to set the pagebreak to match the x items then the y items etc.


To put long story short, I wanna have each criteria on a page.


I need some assistance to resolve this.


Thanks
Kelly


Code:
Sub HPage_Breaks()
Dim rpp As Long, rh As Long, i As Long, lr As Long, lc As Long
lr = Sheet2.Cells.Find("*", , , , xlByRows, xlPrevious).Row
lc = Sheet2.Cells.Find("*", , , , xlByColumns, xlPrevious).Column
    On Error GoTo Cancelled
ActiveWindow.View = xlPageBreakPreview
ActiveWindow.View = xlNormalView


With Sheet2
    .ResetAllPageBreaks
    .PageSetup.Zoom = 50
    .PageSetup.PrintArea = .Range("A2:O" & lr).Address  
    .PageSetup.PrintTitleRows = "$1:$2" 
    .HPageBreaks.Add (.Cells(52, 1))
    For i = 1 To lr / (51)
        .HPageBreaks.Add (.Cells(.HPageBreaks(i).Location.Row + 51, 1))
    Next i
End With
Sheet2.PrintOut
Cancelled:
Exit Sub
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Okay cool. It's working great just that it is separating the headers to a different page.

How do I exclude the headers?
 
Upvote 0
Without seeing what code you are using and the layout of your sheet, I am only guessing.
Presumably your page break loop is starting from row 1 which is a header row.
If so, change the loop so that it starts from the first data row.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top