Search code examples
excelvbapage-break

Page break is landing on the wrong line


My page break is landing in the wrong row. I need it to go up one line. Instead of the page breaks between rows (3 & 4), (10 & 11), (17 & 18), (24 & 25), I need it to land between (2 & 3), (9 & 10), (16 & 17), 23 & 24). The code finds "Shipment Order" then adds HPageBreak below the cell not above it.

Visual of the problem:

enter image description here

Code:

Sub APageBreakbyOrder()

ActiveSheet.ResetAllPageBreaks

Dim SFind As Integer
Dim LR As Integer

    LR = Cells(Rows.Count, 1).End(xlUp).Row

    For SFind = 1 To LR
    If Cells(SFind, 1).Value = "Shipment Order" Then
    ActiveSheet.HPageBreaks.Add Before:=Rows(SFind + 1)
    End If
    Next

End Sub

This code works great, I just need to move the page break line up one for each loop. How can I do this?


Solution

  • If the page break is one row too low, then chances are it will be correct if you remove the + 1 in the line

    ActiveSheet.HPageBreaks.Add Before:=Rows(SFind + 1)