Dynamic Print Area and Page Breaks

RedStarryGirl

New Member
Joined
Jan 26, 2009
Messages
8
Hello,

I have a report in Excel 2007 which is just a pivot table controlled by a dropdown menu that users select a state from and it filters the pivot table. I already have code to dynamically set the print area for the report, as the number of rows varies dependent on the state selected. There are header rows repeated at the top, but what I need accomplished is to set a page break every 70 rows - not including the rows repeated at the top - so that a group of data is not split up. Is there a way to do this, given the Print Area? i.e. set a page break every 70 rows as long as the row is within the determined print area?

The code to set the print area:

Code:
Sub PrintArea()

Dim i As Integer
i = 14
Do While Cells(i, 24) <> ""
    i = i + 1
Loop


With ActiveSheet.PageSetup
        .PrintArea = "A14:Z" & i - 1
        .PrintTitleRows = "$4:$13"
        .PrintTitleColumns = ""
End With
End Sub
 

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.
This code will add a page break every 70 rows starting with row 84, but unless the paper is very large or the row height / font is small, the automatic page breaks will also appear.
Code:
Sub PrintArea()

    Dim i As Integer
    Dim lLastDataRow As Long
    Dim lX As Long
    
    lLastDataRow = Cells(Rows.Count, 24).End(xlUp).Row  'last filled cell in column X
    
    i = 14
    Do While Cells(i, 24) <> ""
        i = i + 1
    Loop
    
    
    With ActiveSheet.PageSetup
            .PrintArea = "A14:Z" & i - 1
            .PrintTitleRows = "$4:$13"
            .PrintTitleColumns = ""
    End With
    
    For lX = 14 + 70 To lLastDataRow Step 70
        ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Cells(lX, 1)
    Next
    
End Sub
 
Upvote 0
Thank you for your help! I actually got some assistance from a colleague and ended up with the following code. I am very much a beginner with VBA so I don't know if there are advantages/disadvantages for using this method over yours as far as efficiency goes. P.S. I had to change my indicator column for finding the last row to print because there were some blanks in it.

Code:
Sub PrintArea()
Dim i As Integer
i = 14
Do While Cells(i, 9) <> ""
    i = i + 1
Loop
i = i + 30
Dim j As Integer
j = 84
Do While j < i - 1
Rows(j & ":" & j).Select
    ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
j = j + 70
Loop
With ActiveSheet.PageSetup
        .PrintArea = "A14:Z" & i - 1
        .PrintTitleRows = "$4:$13"
        .PrintTitleColumns = ""
End With
End Sub

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,215,343
Messages
6,124,402
Members
449,156
Latest member
LSchleppi

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