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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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,045
Messages
6,122,836
Members
449,096
Latest member
Erald

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