VBA Code to Auto Adjust Page Breaks

Justinian

Well-known Member
Joined
Aug 9, 2009
Messages
1,551
Office Version
  1. 365
Platform
  1. Windows
Every day, I export data from an external database and paste into Excel. From there, I run a macro cleaning up formatting and removing bad data but the number of pages varies day to day and the page break always causes a few blank pages to print. How do I input code into my extant macro so only pages with data print?
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
If you are working with a single worksheet, define the print area to cover your filled cells
Code:
    ActiveSheet.PageSetup.PrintArea = "$A$1:$H$19"

If you are working with multiple worksheets, delete the blank ones and optionally define the print area for each remaining sheet.
Code:
Function IsWorksheetBlank(Optional wks As Worksheet) As Boolean
    If wks Is Nothing Then Set wks = ActiveSheet
    If Application.WorksheetFunction.CountBlank(wks.Cells) = _
        Application.WorksheetFunction.Count(wks.Cells) Then
        IsWorksheetBlank = True
    End If
End Function
 
Upvote 0
This code only covers A1:H19 so it is not dynamic.


ActiveSheet.PageSetup.PrintArea = "$A$1:$H$19"
 
Upvote 0
If your data is contiguous with no blank rows or columns use:
ActiveSheet.PageSetup.PrintArea = Activesheet.Range("A1").CurrentRegion

If your cleanup leaves some blank rows or columns, if the data after them should start on a new sheet you can insert a horizontal or vertical page break at the next filled row or column and delete the blank rows/columns before .
 
Upvote 0
My data is contiguous.

I just tried your code and it still prints two pages of blanks.
 
Upvote 0
When you execute this code:
Activesheet.Range("A1").CurrentRegion.Select
It will select the current region. If some of the selection has apparently empty rows/columns, then they may contain spaces or other non-visible characters (or characters colored to match the background) which would print as blanks. If so delete/change font color those columns and run that code again.

If there are any shapes (which may be transparent) then Excel will extend the default print area to print them.

This will reset any manually applied page breaks:
ActiveSheet.ResetAllPageBreaks

Use View | PageBreak Preview to see how Excel distributes the data over the worksheets. Manually adjusting page breaks there to eliminate blank sheets may reveal how to code something to do the same.
 
Upvote 0
So where in this code below should i add your new code?

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Dim lr As Long, lc As Long
lc = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious).Column
lr = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
With Range(Cells(3, 1), Cells(lr, lc))
.Font.Size = 8
.RowHeight = 61.5
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
With ActiveSheet.PageSetup
.PrintTitleRows = "$2:$2"
.PrintTitleColumns = ""
End With[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] Rows("2:2").RowHeight = 20.4
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlTop
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Font.Size = 8[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]'Deletes unwanted columns before sorting
Range("B:B,D:D,H:K,N:N,AA:AA,AD:AE").EntireColumn.Delete
ActiveWindow.View = xlPageBreakPreview
ActiveSheet.PageSetup.PrintArea = "$A$1:$U$159"
ActiveWindow.View = xlNormalView[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]'Sort entire data range by last name based on column G (complete by date)
With ActiveWorkbook.Worksheets(ActiveSheet.Name).Sort
.SortFields.Clear
.SortFields.Add Key:=Range("E2:E" & Cells(Rows.Count, "E").End(xlUp).Row), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range("A2:U" & Cells(Rows.Count, "E").End(xlUp).Row)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Range("A2").Select

'Closes Book1
Windows("Book1.xlsx").Activate
ActiveWindow.Close[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]End Sub[/FONT]
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif][/FONT]
 
Upvote 0
If using
Activesheet.Range("A1").CurrentRegion
in the immediate window
selected your filled cells after the edits then replacing
ActiveSheet.PageSetup.PrintArea = "$A$1:$U$159"
with
ActiveSheet.PageSetup.PrintArea = Activesheet.Range("A1").CurrentRegion
should work.
 
Upvote 0
Odd, that small change made it worse. Before, the report I ran was nine pages with 1.5 pages of blank; with your change, it is now 16 pages with five pages of blank.
 
Upvote 0
Manually delete all columns to the right of the last filled column and below the last filled row and try printing again.
Use Editing | Find & Select | Selection Pane to confirm that there are no objects on the worksheet you are printing.
Are blank pages still being printed?
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,382
Members
448,889
Latest member
TS_711

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