Then this should do the trick
Sub SetHPageBreaks()
Dim LastDataRow As Long
Dim lngRow As Long 'Counter to loop through each row
Dim EvaluatingData As String
Dim SheetName As String
SheetName = ThisWorkbook.Worksheets(1).Name 'Sheet name where the data is located.
LastDataRow = Worksheets(SheetName).Cells(Rows.Count, 2).End(xlUp).Row 'This is the row number of the last cell with data in Column B
EvaluatingData = Worksheets(SheetName).Cells(LastDataRow, 2).Value 'Value inside each cell being evaluated during the loop.
Worksheets(SheetName).ResetAllPageBreaks 'This line will errase all the custom Pagebreaks. if you have custom pagebreaks that you need to keep, then delete.
For lngRow = 1 To LastDataRow 'Modify the 1 with the row number for the first data value.
If Worksheets(SheetName).Cells(lngRow, 2).Value <> EvaluatingData And Worksheets(SheetName).Cells(lngRow, 2).Value <> "" Then
Worksheets(SheetName).HPageBreaks.Add Before:=Rows(Worksheets(SheetName).Cells(lngRow, 2).Row)
EvaluatingData = Worksheets(SheetName).Cells(lngRow, 2).Value
End If
Next
End Sub
Bookmarks