Macro Page Preview

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,603
Office Version
  1. 2021
Platform
  1. Windows
I have the following Code Below. The 1st page break should be row 49, but the break sows at row 44.

It would be appreciated if someone could amend my code so that 1st break is at row 49 and 2nd break to be up to last row containing data

Code:
 Sub Print_Preview()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim lastCol As Long
    Dim printRange As Range
  
    Set ws = Sheets("Summary")
  
    With ws
        ' Find the last row and column containing data
        lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row
        lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column

        ' Define the print range starting from Column F to the last column
        Set printRange = .Range(.Cells(1, "F"), .Cells(lastRow, lastCol))
      
        ' Set up the page setup options
        With .PageSetup
            .PrintTitleRows = "$1:$1" ' Row 1 repeats at top
            .PrintTitleColumns = "$A:$A" ' Column A repeats on each page
            .Orientation = xlLandscape
            .Zoom = False ' Turn off automatic zoom
            .FitToPagesWide = 2 ' Fit to 1 page wide
            .FitToPagesTall = 2 ' Fit to 2 pages tall (70% scale)
            .PrintGridlines = True
          
            ' Headers and footers
            .LeftHeader = "&D&T"
            .CenterHeader = "Turnover Data"
            .RightHeader = "Page &P of &N"
        End With

        ' Reset all existing page breaks
        .ResetAllPageBreaks
      
        ' Insert a manual horizontal page break at row 49
        .HPageBreaks.Add Before:=.Cells(53, "F")

        ' Activate the sheet and display Page Break Preview
        .Activate
        ActiveWindow.View = xlPageBreakPreview
      
        ' Set the print area for the defined range
        .PageSetup.PrintArea = printRange.Address
      
        ' Show Print Preview
        printRange.PrintPreview
    End With
End Sub [code]
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I'm guessing that you have some multiple of 44 rows, like 88, 132, 176? I think your data is being divided evenly into portions, then because you spec 2 pages you're getting a break at 44 (and maybe 88). Does that make sense?

I used your code on a sheet with 120 rows. The breaks are at 60 and 120. Each time I added blocks of rows, the break row is further down but always divided by 2.
 
Upvote 0
Thanks for the help. I created this additional code which works

Code:
 Sub Adjust_PageBreak_Row49()
    Dim ws As Worksheet
    Dim lastRow As Long

    ' Define the worksheet
    Set ws = Sheets("Summary")

    With ws
        ' Find the last row with data
        lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row

        ' Reset all existing page breaks (this will remove the dotted blue lines as well)
        .ResetAllPageBreaks

        ' Add a manual horizontal page break at row 49
        If lastRow > 49 Then
            .HPageBreaks.Add Before:=.Rows(50)
        End If

        ' Add a manual horizontal page break after the last row of data
        If lastRow > 49 Then
            .HPageBreaks.Add Before:=.Rows(lastRow + 1)
        End If

        ' Ensure page setup is configured properly
        With .PageSetup
            .Orientation = xlLandscape
            .PrintGridlines = True
            ' Disable automatic scaling to avoid conflicts
            .FitToPagesWide = False
            .FitToPagesTall = False
        End With
 Set ActiveSheet.HPageBreaks(1).Location = Range("F50")
        ' Switch to Page Break Preview to confirm the change
        .Activate
        ActiveWindow.View = xlPageBreakPreview
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,698
Messages
6,180,423
Members
452,981
Latest member
MarkS1234

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