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

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
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,215,432
Messages
6,124,858
Members
449,194
Latest member
HellScout

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