VBA Insert Page Break based on cell contents

Nyanko

Active Member
Joined
Sep 1, 2005
Messages
437
Hi,

I'm looking for a very quick VBA snippet that will insert a pagebreak everytime it finds a row called "Cumulative Total". I've tried searching and can only seem to find long exampled about setting breaks when values change etc...
Many thanks
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Code:
Sub addpagebreaksatvalue()
Dim x As Integer
Dim LR As Integer
LR = Cells(Rows.Count, 1).End(xlUp).Row
For x = 1 To LR
If Cells(x, 1).Value = "Cumulative Total" Then
ActiveSheet.HPageBreaks.Add Before:=Rows(x + 1)
End If
Next
End Sub

You can use find instead of the loop also
 
Upvote 1
Sub pagebreak()
Dim MyCell As Range
For Each MyCell In Range("A1:A10975")

If MyCell.Value Like "*Page 1 of 1*" Then
ActiveCell.EntireRow.Select
ActiveWindow.SelectedSheets.HPageBreaks.Add ActiveCell.Offset(1, 0)
Else
End If

ActiveCell.Offset(1, 0).Select
Next
End Sub
 
Upvote 0
I really dont know what im doing (obviously).

Could someone please tell me how to manually add a page break before the activesheet.paste line? its seems it should be simple, but i really dont understand the couple of lines BEFORE the activesheet.paste command. I think its looking for the last blank row or cell someway.

Many Thanks in advance.



Sheets("Summary").Select
NextRow = Cells(Rows.Count, 2).End(xlUp).Row + 2
Cells(NextRow, 2).Select
ActiveSheet.Paste
Range("B3").Select
 
Upvote 0
You don't need the .select or activesheet here:

Code:
NextRow = sheets("Summary").Cells(Rows.Count, 2).End(xlUp).Row + 2
sheets("Summary").Cells(NextRow, 2).paste

does the same thing, then you can:

sheets("Summary").HPageBreaks.Add Before:=NextRow +1 or wherever.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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