Public Sub Create_PDF_Pages()
Dim currentSheet As Worksheet
Dim currentView As Long
Dim PDFsheet As Worksheet
Dim destCell As Range
Dim copyRange As Range
Dim pageBreak As HPageBreak
Dim page1StartCell As Range
Dim PDFoutputFile As String
Dim sheetName As String
Dim allPages As Variant, pagesCsv As Variant, pagesRange As Variant
Dim pageNumber As Long
allPages = Split("Sheet2!1-8,Sheet1!1-4,Sheet3!1-6", ",") 'sheet tab names and their page numbers
'allPages = Split("Sheet1!1,Sheet2!1,Sheet3!1,Sheet1!2,Sheet2!2,Sheet3!2", ",") 'example - page 1 of each sheet followed by page 2 of each sheet
PDFoutputFile = "C:\Desktop\Save Folder\" & Worksheets("Sheet2").Range("E600").Value & ".pdf"
Application.ScreenUpdating = False
With ActiveWorkbook
Set currentSheet = .ActiveSheet
Set PDFsheet = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
End With
Set destCell = PDFsheet.Range("A1")
With ActiveWindow
currentView = .View
.View = xlPageBreakPreview
End With
'Copy specified pages to the PDF output sheet
For Each pagesCsv In allPages
sheetName = Split(pagesCsv, "!")(0)
pagesRange = Split(Split(pagesCsv, "!")(1), "-")
With ActiveWorkbook.Worksheets(sheetName)
Set page1StartCell = .UsedRange.Range("A1")
For pageNumber = pagesRange(0) To pagesRange(UBound(pagesRange))
Set pageBreak = .HPageBreaks(pageNumber)
'Set rows for this page and copy them to clipboard
If pageNumber = 1 Then
'Set range to copy for page 1 from the start of page 1 to the page 1 page break
Set copyRange = Range(page1StartCell, pageBreak.Location.Offset(-1)).EntireRow
Else
'Set range to copy for all other pages from the start of the previous page's page break to this page's page break
Set copyRange = Range(.HPageBreaks(pageNumber - 1).Location, pageBreak.Location.Offset(-1)).EntireRow
End If
copyRange.Copy
'Paste to destination cell in PDF output sheet
destCell.Select
destCell.PasteSpecial Paste:=xlPasteColumnWidths, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
destCell.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
destCell.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'Use Format Painter to copy and paste row heights in PDF output sheet
copyRange.EntireRow.Copy
destCell.EntireRow.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
'Update destination cell for next page in PDF output sheet
With PDFsheet
Set destCell = .Cells(.UsedRange.Rows.Count + 1, 1) 'last row in used range
End With
Next
End With
Next
'Save the PDF output sheet as a PDF
With PDFsheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFoutputFile, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
End With
ActiveWindow.View = currentView
currentSheet.Activate
Application.ScreenUpdating = True
MsgBox "Created " & PDFoutputFile
End Sub