Code for inserting page breaks across multiple worksheets?

Milos

Board Regular
Joined
Aug 28, 2016
Messages
121
Office Version
  1. 365
Platform
  1. Windows
I am looking for an amazing solution to a problem that I am having printing.

I want to insert a page-break at the end of each worksheet (I have 50-200 worksheets in one workbook). Each worksheet is different in length. The end of each worksheet can be identified by the first blank cell/row without any values.

Is there any way to magically code this?

Thanks in advance!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Code:
[color=darkblue]Sub[/color] PageBreaks()
    [color=darkblue]Dim[/color] rngLastRow [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] ws [color=darkblue]As[/color] Worksheet
    
    [color=darkblue]For[/color] [color=darkblue]Each[/color] ws [color=darkblue]In[/color] Worksheets
        [color=darkblue]Set[/color] rngLastRow = ws.Cells.Find("*", , , , 1, 2)
        [color=darkblue]If[/color] [color=darkblue]Not[/color] rngLastRow [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color]
            ws.HPageBreaks.Add Before:=rngLastRow.Offset(1)
        [color=darkblue]End[/color] [color=darkblue]If[/color]
    [color=darkblue]Next[/color] ws
    
    MsgBox "Page Breaks added."
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Thanks works perfectly. AlphaFrog you are an absolute legend!
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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