Page break for condition

i8ur4re

Board Regular
Joined
Mar 1, 2015
Messages
97
Good day, I have an excel sheet that ha various prices. I would like to add a page break for each page. The way i have it setup is as so.


ITEMDESCRIPTIONPAGEMSRPCASHCREDIT12 Months18 Months24 Months
202391QFQUEEN/FULL HEADBOARD (WARM BROWN)Pg.22$155.99$135.99$149.99$14.99$10.998.99
202391KEE KING HEADBOARD (WARM BROWN)Pg.22$232.99$203.99$224.99$22.99$16.9913.99
202391TTWIN HEADBOARD (WARM BROWN)Pg.22$141.99$123.99$136.99$13.99$9.998.99
202392NIGHT STAND (WARM BROWN)Pg.22$134.99$117.99$129.99$12.99$9.997.99
202393DRESSER (WARM BROWN)Pg.22$376.99$329.99$362.99$36.99$26.9921.99
202394MIRROR (WARM BROWN)Pg.22$84.99$73.99$81.99$8.99$5.994.99
202395CHEST (WARM BROWN)Pg.22$319.99$279.99$307.99$30.99$22.9918.99
Q 4PC SET (Q/F.HEAD,NS,DR,MR)$750.99$656.99$722.99$72.99$52.9942.99
Q 5PC SET (Q/F.HEAD,NS,DR,MR,CH)$1,069.99$935.99$1,029.99$102.99$74.9960.99
MSRPCASHCREDIT12 Months18 Months24 Months
202081QQUEEN BED (CAPPUCCINO)Pg.23$262.99$229.99$252.99$25.99$18.9914.99
202082NIGHT STAND (CAPPUCCINO)Pg.23$136.99$119.99$131.99$13.99$9.997.99
202083DRESSER (CAPPUCCINO)Pg.23$387.99$339.99$373.99$37.99$26.9921.99
202084MIRROR (CAPPUCCINO)Pg.23$91.99$79.99$87.99$8.99$6.995.99
202085CHEST (CAPPUCCINO)Pg.23$305.99$267.99$294.99$29.99$21.9917.99
Q 4PC SET (Q.BED,NS,DR,MR)$877.99$768.99$845.99$84.99$61.9949.99
Q 5PC SET (Q.BED,NS,DR,MR,CH)$1,183.99$1,035.99$1,139.99$113.99$82.9966.99

<tbody>
</tbody>


I would like to add a page break between pg.22 and pg.23. The pages run all the way to around 300 pages. Can you please help me, i have tried so many other websites but still cant seem to get it to work. Thank you in advance
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
The following macro assumes that the sheet containing the data is the active sheet, and that the data starts at A1. It uses Column D to determine where to add the page breaks. It adds a page break at the row prior to each occurrence of "MSRP".

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> AddPageBreaks()<br><br>    <SPAN style="color:#00007F">Dim</SPAN> rFoundCell <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> sFirstAddress <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> lCnt <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    ActiveSheet.ResetAllPageBreaks<br>    <br>    <SPAN style="color:#00007F">With</SPAN> ActiveSheet.Columns("D")<br>        <SPAN style="color:#00007F">Set</SPAN> rFoundCell = .Find(what:="MSRP", after:=.Rows(.Rows.Count), LookIn:=xlValues, lookat:=xlWhole, searchdirection:=xlNext, MatchCase:=False)<br>        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> rFoundCell <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>            lCnt = 0<br>            sFirstAddress = rFoundCell.Address<br>            <SPAN style="color:#00007F">Do</SPAN><br>                lCnt = lCnt + 1<br>                <SPAN style="color:#00007F">If</SPAN> lCnt > 1 <SPAN style="color:#00007F">Then</SPAN><br>                    ActiveSheet.HPageBreaks.Add before:=rFoundCell<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> rFoundCell = .FindNext(rFoundCell)<br>            <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">While</SPAN> rFoundCell.Address <> sFirstAddress<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> rFoundCell = <SPAN style="color:#00007F">Nothing</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Hope this helps!
 
Upvote 0
The following macro assumes that the sheet containing the data is the active sheet, and that the data starts at A1. It uses Column D to determine where to add the page breaks. It adds a page break at the row prior to each occurrence of "MSRP".

Option Explicit

Sub AddPageBreaks()

****Dim rFoundCell As Range
****Dim sFirstAddress As String
****Dim lCnt As Long
****
****ActiveSheet.ResetAllPageBreaks
****
****With ActiveSheet.Columns("D")
********Set rFoundCell = .Find(what:="MSRP", after:=.Rows(.Rows.Count), LookIn:=xlValues, lookat:=xlWhole, searchdirection:=xlNext, MatchCase:=False)
********If Not rFoundCell Is Nothing Then
************lCnt = 0
************sFirstAddress = rFoundCell.Address
************Do
****************lCnt = lCnt + 1
****************If lCnt > 1 Then
********************ActiveSheet.HPageBreaks.Add before:=rFoundCell
****************End If
****************Set rFoundCell = .FindNext(rFoundCell)
************Loop While rFoundCell.Address <> sFirstAddress
********End If
****End With
****
****Set rFoundCell = Nothing
****
End Sub


Hope this helps!


I tried this just now, i got nothing. Would i be able to send you the excel sheet, i have tried everything.
 
Upvote 0
Are my assumptions correct?

Did you make any changes to the code?

Does it help if you replace...

Code:
Set rFoundCell = .Find(what:="MSRP", after:=.Rows(.Rows.Count), LookIn:=xlValues, lookat:=xlWhole, searchdirection:=xlNext, MatchCase:=False)

with

Code:
Set rFoundCell = .Find(what:="MSRP", after:=.Rows(.Rows.Count), LookIn:=xlValues, lookat:=[COLOR="#FF0000"]xlPart[/COLOR], searchdirection:=xlNext, MatchCase:=False)
 
Upvote 0
Are my assumptions correct?

Did you make any changes to the code?

Does it help if you replace...

Code:
Set rFoundCell = .Find(what:="MSRP", after:=.Rows(.Rows.Count), LookIn:=xlValues, lookat:=xlWhole, searchdirection:=xlNext, MatchCase:=False)

with

Code:
Set rFoundCell = .Find(what:="MSRP", after:=.Rows(.Rows.Count), LookIn:=xlValues, lookat:=[COLOR=#FF0000]xlPart[/COLOR], searchdirection:=xlNext, MatchCase:=False)


That still didnt work for me. Im not sure what i am doing wrong. Any other suggestions?
 
Upvote 0
Were my initial assumptions correct?

Did you make any changes to the code? If so, can you post the exact code?
 
Upvote 0
Were my initial assumptions correct?

Did you make any changes to the code? If so, can you post the exact code?



I copied what you provided me and pasted it. When i switched over to page break layout, it didnt show the pages broken at the msrp column.
 
Upvote 0
Since you didn't make any changes to the code, I'm assuming that my earlier assumptions were correct. That is, the sheet containing the data is the active sheet, the data starts at A1, and Column D is the "MSRP" column.

This probably won't make a difference, but it's worth trying...

<font face=Calibri><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br><br><SPAN style="color:#00007F">Sub</SPAN> AddPageBreaks()<br><br>    <SPAN style="color:#00007F">Dim</SPAN> CurrView <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> rFoundCell <SPAN style="color:#00007F">As</SPAN> Range<br>    <SPAN style="color:#00007F">Dim</SPAN> sFirstAddress <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> lCnt <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <br>    CurrView = ActiveWindow.View<br>    <br>    ActiveWindow.View = xlPageBreakPreview<br>    <br>    ActiveSheet.ResetAllPageBreaks<br>    <br>    <SPAN style="color:#00007F">With</SPAN> ActiveSheet.Columns("D")<br>        <SPAN style="color:#00007F">Set</SPAN> rFoundCell = .Find(what:="MSRP", after:=.Rows(.Rows.Count), LookIn:=xlValues, lookat:=xlPart, searchdirection:=xlNext, MatchCase:=False)<br>        <SPAN style="color:#00007F">If</SPAN> <SPAN style="color:#00007F">Not</SPAN> rFoundCell <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN><br>            lCnt = 0<br>            sFirstAddress = rFoundCell.Address<br>            <SPAN style="color:#00007F">Do</SPAN><br>                lCnt = lCnt + 1<br>                <SPAN style="color:#00007F">If</SPAN> lCnt > 1 <SPAN style="color:#00007F">Then</SPAN><br>                    ActiveSheet.HPageBreaks.Add before:=rFoundCell<br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <SPAN style="color:#00007F">Set</SPAN> rFoundCell = .FindNext(rFoundCell)<br>            <SPAN style="color:#00007F">Loop</SPAN> <SPAN style="color:#00007F">While</SPAN> rFoundCell.Address <> sFirstAddress<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>    ActiveWindow.View = CurrView<br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    <br>    <SPAN style="color:#00007F">Set</SPAN> rFoundCell = <SPAN style="color:#00007F">Nothing</SPAN><br>    <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Does this help?
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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