Insert Page Breaks with Offset from Cell Value & Insert Page Breaks to Table with Varying Number of Rows

bearwires

Board Regular
Joined
Mar 25, 2008
Messages
57
Office Version
  1. 365
Platform
  1. Windows
I've been trying to get page breaks automatically inserted based on text within a specific cell.
Most page breaks will use the text string "Project :" within the headers to insert a page break 4 rows above at the top of the page header.
The cell which contains this "Project :" is either the text string itself, or a formula whose result is this text string.

I have included a link to a sample document below, hoping someone can help me out.
I've marked on the right hand side of this document where I need the page breaks to be inserted.
Most page breaks required at the headers but for the large table, I need page breaks to be inserted so that it fits enough table rows to fill each page to minimise white space on each page.
In the original document, the entire table content is populated using formulae to pull info from a separate master worksheet, but the result in each table cell is either a text string or a numerical value.

Example Excel Test File

Below is the VBA code I currently put together using various sources, mostly from MrExcel.com
Whilst it doesn't give any runtime errors, it doesn't do what I need it to.
My VBA skills are pretty much non-existent so I am hoping some VBA wizard here will be able to help me make it work and achieve the desired outcome.
Your help with this would be very much appreciated.

VBA Code:
Sub InsertPageBreaks()
Dim c As Range

Application.ScreenUpdating = False

ActiveWindow.View = xlNormalView

With ActiveSheet
    .Unprotect Password:="password"
    .ResetAllPageBreaks
    .Cells.PageBreak = xlPageBreakNone
    .Columns("D").Insert
    .Columns("C").Copy .Columns("D")
    .Columns("D").Replace "Project :", "#N/A"
    On Error Resume Next
    For Each c In .Columns("D").SpecialCells(2, xlErrors)
        c.Offset(-4, 0).PageBreak = xlPageBreakManual
    Next c
    .Columns("D").Delete
    .Protect Password:="password"
End With

Application.ScreenUpdating = True

'
MsgBox "   PAGE BREAKS SET!!" & vbCrLf & "          Press OK"                                                  


End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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