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.
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