Macro to insert rows before Page Breaks

redjay

New Member
Joined
May 9, 2008
Messages
46
Hi all

I am using the subtotal function to insert page breaks.

I would like some code which will search for the page breaks in my worksheet and automatically insert 2 blank rows above each Page Break.

Might this be possible?

Many thanks
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
This macro should do the trick:

Code:
Option Explicit
Sub InsertRowsBeforePageBreak()
    Dim sh As Worksheet
    Set sh = ActiveSheet
    Dim nRowsAdded As Long
    Dim i As Long
    For i = 1 To sh.HPageBreaks.Count
        Rows(Range(sh.HPageBreaks(i).Location).Row + nRowsAdded & ":" & Range(sh.HPageBreaks(i).Location).Row + nRowsAdded + 1).Insert Shift:=xlDown
        nRowsAdded = nRowsAdded + 2
    Next i
End Sub

Hope this helped,
Rolf
 
Upvote 0

Forum statistics

Threads
1,215,553
Messages
6,125,483
Members
449,233
Latest member
Deardevil

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