Home » excel » Excel VBA Save multiple sheets from a range to a single PDF

Excel VBA Save multiple sheets from a range to a single PDF

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m currently working on workbook where in column A:A of worksheet(“STAM-Filialen”) nearly all the names of the other worksheets are. I want only those worksheets named in column(“A:A”) in a single PDF. The code I use know makes it a separate file for each worksheet. Is it possible to use a sort of a same code to save it as a single PDF?

Dim myCell As Range
Dim lastCell As Long
Dim PathName As String
lastCell = lastRow("STAM-Filialen")
PathName = Range("I10").Value

Worksheets("STAM-Filialen").Activate
For Each myCell In ThisWorkbook.Worksheets("STAM-Filialen").Range("A2:A" & lastCell).Cells
    Dim wksName As String
    wksName = myCell.Text
    ThisWorkbook.Worksheets(wksName).Range("A1:P60").ExportAsFixedFormat Type:=xlTypePDF, Filename:=PathName & "DispoPlan.Filiaal " & wksName & ".PDF"
Next
How to&Answers:

I’d recommend moving all the values to a single sheet to print. Then delete this temporary sheet when done.

Here’s an example of placing each range from each sheet side by side in a new sheet.

Option Explicit

Public Sub CreateSinglePDF()
    Dim ws As Range: Set ws = ThisWorkbook.Sheets(1).Range("A1:A4")
    Dim rangeDict As Object: Set rangeDict = CreateObject("Scripting.Dictionary")
    Dim cell As Range

    For Each cell In ws
        If Not rangeDict.exists(cell.Value) And cell.Value <> "" Then
            rangeDict.Add cell.Value, ThisWorkbook.Sheets(cell.Value).Range("A1:A5")
        End If
    Next

    Dim printsheet As Worksheet
    Set printsheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count))

    Dim key As Variant
    Dim i As Long: i = 1
    For Each key In rangeDict
        printsheet.Range(printsheet.Cells(1, i), printsheet.Cells(5, i)).Value = rangeDict(key).Value
        i = i + 1
    Next

    printsheet.UsedRange.ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\users\ryan\desktop\ExampleFile.pdf"
    printsheet.Delete
End Sub

Answer:

To convert multiple sheets into single pdf document,

  1. first select multiple sheets
  2. and use the Activesheet.ExportAsFixedFormat statement.
  3. The print range of the page can be set in Page Setup.

Code

   Sub test()
        Dim WB As Workbook
        Dim Ws As Worksheet
        Dim sht As Worksheet
        Dim PathName As String
        Dim vWs() as String '<~~ Variable change
        Dim rngDB As Range, rng As Range
        Dim n As Integer

        Set WB = ThisWorkbook
        Set Ws = WB.Worksheets("STAM-Filialen")

        PathName = Range("I10").Value

        With Ws
            Set rngDB = .Range("a1", .Range("a" & Rows.Count).End(xlUp))
        End With

        For Each rng In rngDB
            n = n + 1
            ReDim Preserve vWs(1 To n)
            vWs(n) = rng.text '<~~ text
            Set sht = Sheets(rng.Value)
            With sht.PageSetup
                .PrintArea = "a1:p60"
            End With
        Next rng
        Sheets(vWs).Select '<~~ multiple sheets select
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=PathName & "DispoPlan.Filiaal.PDF"

    End Sub

Worksheets(“STAM-Filialen”)

enter image description here

Specipic Sheets selected

enter image description here

Single pdf

enter image description here