Printing issue

xsmurf

Board Regular
Joined
Feb 24, 2007
Messages
55
Office Version
  1. 365
Platform
  1. Windows
Hi,

I always appreciate the help that I get on this forum, and I hope you guys/ladies can help me out.

I use below code to print an excel sheet to a pdf file and then sent it through email. This works fine.
The problem I have is that when I print the ranges, they don't always show up perfect in the PDF.

The ranges that I have are "$A$2:$I$43,$A$45:$I$86,$A$88:$I$129,$A$131:$I$172,$A$174:$I$215"
This is only a sample of the ranges, in total there can be 12 ranges, and some can be hidden. Only the visible ranges will be printed.

I use column "J" as a reference to find the last visible row.

The main thing that needs to happen is that the ranges will be printed on 1 sheet only (ranges all have the same size).
I can have a pdf file that ranges between 1 page or 12 depending on the user input.
If more information is needed please let me know.



VBA Code:
Sub Insert_pagebreaks_pagesetup(Optional hidden As Boolean)

    Dim lLastColumn As Long
    Dim lLastRow    As Long
    Dim HPBreak As HPageBreak
    Dim i As Long
    Dim RowsPerPage As Integer
    RowsPerPage = 53          'you want to print 45 rows per page
    
    With ActiveSheet
    .ResetAllPageBreaks
'       Dynamically set print area
        lLastColumn = .Range("J1").End(xlToLeft).Column
        lLastRow = .Cells(Rows.Count, "J").End(xlUp).row
        .PageSetup.PrintArea = Range(.Cells(1, 9), .Cells(lLastRow, lLastColumn)).Address
    For i = RowsPerPage + 1 To lLastRow Step RowsPerPage
        On Error Resume Next
        For Each HPBreak In .HPageBreaks
            .Cells(i, 1).EntireRow.Select
            ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=ActiveCell
        Next HPBreak
    Next i
    End With

    Rows("1:1").Select
    Selection.RowHeight = 25 
    Selection.Font.Size = 11
    Rows("44:44,87:87,130:130").Select
    Selection.RowHeight = 35
    Rows("2:2").Select
    Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Selection.RowHeight = 11 
    Range("J2").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("A1").Select
    
    With ActiveSheet.PageSetup
        .PrintTitleRows = "$1:$1"
        .FitToPagesWide = 1
        .FitToPagesTall = False
        .Zoom = 102 '
        .Orientation = xlPortrait
        .CenterHorizontally = True
        .CenterVertically = False
        .CenterFooter = "Page &P of &N"
    End With
    Range("A1").Select
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,223,885
Messages
6,175,180
Members
452,615
Latest member
bogeys2birdies

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