Inserting Page Breaks using VBA

stephensmart

New Member
Joined
Aug 2, 2011
Messages
3
I have a spreadsheet that prints a completed form that can be a variable number of rows long depending on the responses to questions answered by the user.

I need code that will insert page breaks every 60 rows but not split the tables of data - the tables can vary in size based on the responses to the questions.

There is a blank row between each table.

The maximum number of rows on the spreadsheet is 300.

Nothing I have tried so far has worked
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> PageBreaks60()<br>    <SPAN style="color:#00007F">Dim</SPAN> PBRow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, Lastrow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, r <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    <SPAN style="color:#007F00">' reset previous pagebreaks</SPAN><br>    ActiveSheet.ResetAllPageBreaks<br>    <br>    PBRow = 1<br>    Lastrow = Range("A" & Rows.Count).End(xlUp).Row<br>    <SPAN style="color:#00007F">Do</SPAN><br>        <SPAN style="color:#00007F">For</SPAN> r = PBRow + 59 <SPAN style="color:#00007F">To</SPAN> PBRow <SPAN style="color:#00007F">Step</SPAN> -1<br>            <SPAN style="color:#00007F">If</SPAN> Cells(r, "A").Value = "" <SPAN style="color:#00007F">Then</SPAN><br>                PBRow = r + 1<br>                <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">For</SPAN><br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> r<br>        ActiveSheet.HPageBreaks.Add Before:=Rows(PBRow)<br>    <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">Until</SPAN> PBRow + 59 >= Lastrow<br><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0
Thanks Alphafrog - this almost works but its still inserting page breaks every 60 rows and splitting my tables.

I have merged cells in column P that match the size of the tables - perhaps these could be used in some way to prevent the page breaks splitting the tables.
 
Upvote 0
Alphafrog - your code works perfectly thanks - discovered I had blanks in my tables which was causing my problem.

Now I have removed them it does exactly what I need.

You are a life saver as I have spent hours trying to resolve this.

Thank you so much :)
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,445
Members
452,915
Latest member
hannnahheileen

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