Macro to set page breaks

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
Office Version
  1. 2021
Platform
  1. Windows
I have the following code below to set page breaks for a range called "Journals"

The page breaks in the range Journals are to show the breaks as follows to be Row 12:66, 67:118, 119:162, 163:180


Code:
 Sub SetPageBreaksForJournals()

    Dim rng As Range
    Dim i As Long
    
    'Set the range "Journals"
    Set rng = Range("Journals")
    
    'Clear existing page breaks in the range
    rng.ResetAllPageBreaks
    
    'Set new page breaks
    rng.Rows(1).PageBreak = xlPageBreakManual 'Start the first page at row 1
    
    'Loop through each set of rows and set page breaks
    For i = 12 To 163 Step 55 'Increment by 55 to cover the desired range
        rng.Rows(i).PageBreak = xlPageBreakManual
    Next i
    
    'Set the last page break to the end of the range
    rng.Rows(181).PageBreak = xlPageBreakManual 'End the last page at row 181
    
End Sub


However when running the code, I get a run time error "Object does'nt support this property or object" and the code below is highlighted

Code:
 rng.ResetAllPageBreaks


It would be appreciated if someone could amend my code
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
hi there

Try the code below:

VBA Code:
Sub SetPageBreaksForJournals()

Dim rng As Range
Dim i As Long

'Set the range "Journals"
Set rng = Range("Journals")

'Clear existing page breaks in the range
rng.Worksheet.ResetAllPageBreaks

'Set new page breaks
rng.Rows(1).PageBreak = xlPageBreakManual 'Start the first page at row 1

'Loop through each set of rows and set page breaks
For i = 12 To 163 Step 55 'Increment by 55 to cover the desired range
    rng.Rows(i).PageBreak = xlPageBreakManual
Next i

'Set the last page break to the end of the range
rng.Rows(181).PageBreak = xlPageBreakManual 'End the last page at row 181

end sub
 
Upvote 0
Thanks for the help.

When runing your code, I get a run time error "Unable to set the pagebreak property of the range class

the code below is highlighted

Code:
 rng.Rows(1).PageBreak = xlPageBreakManual 'Start the first page at row 1


Kindly amend
 
Upvote 0
Try the below... Tested and not sure how your page layout must look but code below works without error...

VBA Code:
Sub SetPageBreaksForJournals()

Dim rng As Range
Dim i As Long

'Set the range "Journals"
Set rng = Range("Journals")

'Clear existing page breaks in the range
rng.Worksheet.ResetAllPageBreaks

'Set new page breaks
rng.Rows(2).PageBreak = xlPageBreakManual 'Start the first page at row 1

'Loop through each set of rows and set page breaks
For i = 12 To 163 Step 55 'Increment by 55 to cover the desired range
    rng.Rows(i).PageBreak = xlPageBreakManual
Next i

'Set the last page break to the end of the range
rng.Rows(181).PageBreak = xlPageBreakManual 'End the last page at row 181

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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