VBA to set page breaks

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
551
I assembled this code a while ago to create a printable recap report on 8.5 x 11 paper.
It almost works to produce my desired result…the exception:

Sheets(“Gross and Net”) will usually fill an entire sheet of paper.
But, Sheets(“G1-N3”) typically have only 20 lines max each.
Therefore, I must manually modify the Page Breaks and then print as PDF.

Any suggestions on how to set page breaks so that G1 & N1 will be printed on one page…
And ditto, G2 & N2
And, G3 & N3?

Code:
Sub MacroCreateRecap()
 
    myrange = Cells(Rows.Count, 22).End(xlUp).Address
   
    For Each ws In Sheets(Array("GROSS", "NET", "G1", "N1", "G2", "N2", "G3", "N3"))
      ActiveSheet.PageSetup.PrintArea = "$A$1:" & myrange
     
      Next ws
 
    Sheets("Recap").Select
   
    For Each ws In Sheets(Array("GROSS", "NET", "G1", "N1", "G2", "N2", "G3", "N3"))
        LastRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
       
       Cells(Rows.Count, "A").End(xlUp).Offset(1).Select
    ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
 
        ws.Range("A1:V" & LastRow).Copy _
            Destination:=Sheets("RECAP").Range("A" & Rows.Count).End(xlUp).Offset(2)
 
    Next ws
 
  Application.DisplayAlerts = False
  Application.ScreenUpdating = False
   
‘Clean up the formatting
Range("A1") = "ReCap"
        Columns("A:V").Select
         Selection.ColumnWidth = 3
          Columns("A").AutoFit
           Columns("V").AutoFit
             Rows("2").Select
             Selection.Delete Shift:=xlUp
           Range("A1").Select
         Range("U2").ClearContents
 
 
MyDateString = Format(ActiveSheet.Range("A3").Value, "yymmdd")
   
Dim s As String
    s = ActiveSheet.Range("A2")
Dim d As String
    d = MyDateString
 ActiveSheet.Name = d & " " & s
 
‘More code follows to print as pdf and save
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
4,660
Office Version
365
Platform
Windows
Excel is programmed to print separate sheets separately - non negotiable :eek:

So use VBA to
- create a tempoaray sheet (temp)
- paste whatever needs combining from 2 sheets to temp
- print temp
- delete temp
 

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
551
Thanks Yongle.
With a bit of code manipulation, I have resolved the issue

Code:
For Each ws In Sheets(Array("G1", "N1"))
        LastRow = ws.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
       Cells(Rows.Count, "A").End(xlUp).Offset(1).Select
 
        ws.Range("A1:V" & LastRow).Copy _
            Destination:=Sheets("RECAP").Range("A" & Rows.Count).End(xlUp).Offset(2)
 Next ws
 
Cells(Rows.Count, "A").End(xlUp).Offset(1).Select
    ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell

Etc...Etc
 

Forum statistics

Threads
1,081,969
Messages
5,362,484
Members
400,677
Latest member
champchamp

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top