Dynamic Page Breaks

1968rm

Board Regular
Joined
Dec 22, 2010
Messages
172
Hi,

I would like to set horizontal page breaks in vba.
For each cell in range (A7 through A286, where 286 is the value in cell B6), if the cell value is "PB", I need a page break above that cell.
I've tried numerous codes I found, but nothing worked for me.
Any help would be greatly appreciated.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Got one part of it!
Code:
Sub PB()
Dim PBRange As Range, PB As Range
    With ActiveSheet
        Set PBRange = .Range("A7:A1000")
        For Each PB In PBRange
            If PB.Value = "PB" Then
                .HPageBreaks.Add before:=PB
            End If
        Next
    End With
End Sub

I would like to set PBRange to be A7:A286 where 286 is the value in cell B6

I'm sure there's a way, just need to figure out how...
 
Upvote 0
Got no help, had to figure it out myself...;)
Code:
Dim LastRow As Long
LastRow = Range("B6").Value
Set PBRange = .Range("A7:A" & LastRow)
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,828
Members
452,946
Latest member
JoseDavid

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