Find and Move Last hpagebreak

mharper90

Board Regular
Joined
May 28, 2013
Messages
117
Office Version
  1. 365
Platform
  1. MacOS
I have a 3 row tall signature block that is inserted 4 rows below the bottom of a dynamic table. I want to insert code that will ensure that the signature block never gets printed between 2 pages.

I'm looking for code that will find the location of the last hpagebreak, compare it to the location of the last row on the sheet (the bottom of the signature block), and if the difference between those locations is less than 4 rows, then it would relocate the last hpagebreak up the page by 4 rows.

This way, if the entire signature block doesn't fit at the bottom of the last page of the table, then it will be forced to print intact at the top of the next page instead.
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try this macro:
Code:
Public Sub Move_Last_Page_Break()

    Dim saveActiveCell As Range
    Dim lastRow As Long
    Dim lastPageBreak As HPageBreak
        
    Set saveActiveCell = ActiveCell
    
    With ActiveWorkbook.ActiveSheet
        
        'Select last cell so that Excel recalculates automatic page breaks. Can prevent "Subscript out of range" error when referring to HPageBreaks
        'https://support.microsoft.com/en-us/help/210663/you-receive-a-subscript-out-of-range-error-message-when-you-use-hpageb
        
        lastRow = .UsedRange.Rows.Count - .UsedRange.Row + 1
        .Cells(lastRow, "A").Select
        
        Set lastPageBreak = .HPageBreaks(.HPageBreaks.Count)
        If lastRow >= lastPageBreak.Location.Row And lastRow - lastPageBreak.Location.Row < 2 Then
            If lastPageBreak.Type = xlPageBreakAutomatic Then lastPageBreak.Type = xlPageBreakManual
            lastPageBreak.Delete
            .HPageBreaks.Add before:=.Cells(lastRow - 2, "A")
            MsgBox "Last page break moved to above row " & lastRow - 2
        Else
            MsgBox "Last page break not moved from above row " & lastPageBreak.Location.Row
        End If
        
    End With
    
    saveActiveCell.Select
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,802
Members
449,095
Latest member
m_smith_solihull

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