page break help

000

Board Regular
Joined
Jun 3, 2011
Messages
59
Is there any code out there that could help me insert page breaks into a bill of materials? Perhaps it could insert a page break every 70 rows and be able to determine where the last row is that has text in it and insert a page break there.

Thanks for the help!!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
here's something from the help file

Range.PageBreak Property

Returns or sets the location of a page break. Can be one of the following XlPageBreak constants: xlPageBreakAutomatic, xlPageBreakManual, or xlPageBreakNone. Read/write Long. Syntax
expression.PageBreak
expression A variable that represents a Range object.

Remarks
This property can return the location of either automatic or manual page breaks, but it can only set the location of manual breaks (it can only be set to xlPageBreakManual or xlPageBreakNone).
To remove all manual page breaks on a worksheet, set <CODE>Cells.PageBreak</CODE> to xlPageBreakNone.


Example
This example sets a manual page break above row 25 on Sheet1.

<TABLE><TBODY><TR><TH>Visual Basic for Applications</TH></TR><TR><TD>

<CODE>Worksheets("Sheet1").Rows(25).PageBreak = xlPageBreakManual</CODE>
</PRE></TD></TR></TBODY></TABLE></P>This example sets a manual page break to the left of column J on Sheet1.

<TABLE><TBODY><TR><TH>Visual Basic for Applications</TH></TR><TR><TD>

<CODE>Worksheets("Sheet1").Columns("J").PageBreak = xlPageBreakManual</CODE>
</PRE></TD></TR></TBODY></TABLE></P>This example deletes the two page breaks that were set in the preceding examples.

<TABLE><TBODY><TR><TH>Visual Basic for Applications</TH></TR><TR><TD>

<CODE>Worksheets("Sheet1").Rows(25).PageBreak = xlPageBreakNoneWorksheets("Sheet1").Columns("J").PageBreak = xlNone</CODE>
</PRE></TD></TR></TBODY></TABLE></P>Obviously its VBA
 
Upvote 0
Try

Code:
Sub test()
Dim LR As Long, i As Long
For i = 1 To LR Step 70
    ActiveSheet.HPageBreaks.Add Before:=Range("A" & i)
Next i
ActiveSheet.HPageBreaks.Add Before:=Range("A" & LR + 1)
End Sub
 
Upvote 0
That code did not work. It has a run time error '1004':
Application-defined or object defined error

It had a problem with this line:
ActiveSheet.HPageBreaks.Add Before:=Range("A" & LR + 1)
 
Last edited:
Upvote 0
Sorry, try

Code:
Sub pagebreaktest()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR Step 70
    ActiveSheet.HPageBreaks.Add Before:=Range("A" & i)
Next i
ActiveSheet.HPageBreaks.Add Before:=Range("A" & LR + 1)
End Sub
 
Upvote 0
ActiveSheet.HPageBreaks.Add Before:=Range("A" & i)

Now this is the problem. Any thoughts? Thanks again!!
 
Upvote 0
Testes and working

Code:
Sub pagebreaktest()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR Step 70
    ActiveSheet.PageSetup.HPageBreaks.Add Before:=Range("A" & i)
Next i
ActiveSheet.HPageBreak.Add Before:=Range("A" & LR + 1)
End Sub
 
Last edited:
Upvote 0
Testes and working

Rich (BB code):
Sub pagebreaktest()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR Step 70
    ActiveSheet.PageSetup.HPageBreaks.Add Before:=Range("A" & i)
Next i
ActiveSheet.HPageBreak.Add Before:=Range("A" & LR + 1)
End Sub

RED lines are different

Rich (BB code):
Sub pagebreaktest()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR Step 70
    ActiveSheet.PageSetup.HPageBreaks.Add Before:=Range("A" & i)
Next i
ActiveSheet.HPageBreak.Add Before:=Range("A" & LR + 1)
End Sub
 
Upvote 0
The program is still having problems with the red lines. I really am not sure how to fix this.
 
Upvote 0

Forum statistics

Threads
1,224,531
Messages
6,179,379
Members
452,907
Latest member
Roland Deschain

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