How to distribute automatic page breaks between hard page breaks?

JacekKotowski

New Member
Joined
Aug 23, 2013
Messages
18
I am lookign for a sort of Orphan/Widow print control in Excel. I am printing long tables with totals for groups. (The table is a result of an sql query and not a subtotal function in Excel/) I managed to implement a procedure to recognize the text "Total" in column C and insert a HPageBreak right below:
<code>Dim cellTotal As Excel.Range

ActiveSheet.ResetAllPageBreaks
For Each cellTotal In Range("C6", Range("C65000").End(xlUp))
If InStr(cellTotal.Value, "Total") > 0 And Not InStr(cellTotal(3, 1).Value, "Grand Total") > 0 And Not cellTotal.Value = "Grand Total" Then
ActiveSheet.HPageBreaks.Add (cellTotal(3, 1))
End If


Next cellTotal</code>

However, sometimes Excel inserts an automatic pagebreak (marked with blue dashed line). Oftentimes, such an automatic pagebreak falls too close to the hard pagebreak inserted by a macro . Is it somehow possible to rerun the above macro to check if automatic page breaks appear to close to the hard breaks and move them away, eg. ca 3 lines upwards from the pagebreak introduced by the VBA procedure, so that pages do not print with Total only or 1 -two rows with a Total:


In brief, I am thinking of a macro that does: "If automatic pagebreak closer than three lines to manual pagebreak then move the automatic pagebreak 3 or x rows higher than the hard pagebreak placed by the vba macro."


I am thinking of a simple procedure that would go again through the range affected by the above macro and move the automatic page breaks away, upwards from VBA introduced hard pagebreaks if they are too close.

Unfortunately I do not know how to chceck with vba if an automatic pagebreak is inserted by Excel and how to move it away.
Thank you in advance for any indication how to move forward.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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