Search code examples
excelvbapdfexport

Export selected sheets into separate PDF files


I need to export selected sheets in my workbook to separate PDF files.

The code below exports the whole selection of sheets into one PDF and duplicates the PDF with different sheet names.

Sub ExportAsPDF()

Dim Folder_Path As String

With Application.FileDialog(msoFileDialogFolderPicker)
    .Title = "Select Folder path"
    If .Show = -1 Then Folder_Path = .SelectedItems(1)
End With

If Folder_Path = "" Then Exit Sub

Dim sh As Worksheet

For Each sh In ActiveWindow.SelectedSheets
    sh.ExportAsFixedFormat xlTypePDF, Folder_Path & Application.PathSeparator & Range("RD").Text & " " & sh.Name & " 2024.pdf"
Next

MsgBox "Done"

End Sub

Solution

  • Option Explicit
    
    Sub ExportAsPDF()
    
        Dim Folder_Path As String
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Select Folder path"
            If .Show = -1 Then Folder_Path = .SelectedItems(1)
        End With
        If Folder_Path = "" Then Exit Sub
        
        Dim sh As Worksheet, n As Long
        For Each sh In ActiveWindow.SelectedSheets
            sh.Activate
            sh.Select
            ActiveSheet.ExportAsFixedFormat xlTypePDF, _
            Folder_Path & Application.PathSeparator & Range("RD").Text & " " & sh.Name & " 2024.pdf", _
            IgnorePrintAreas:=True
            n = n + 1
        Next
        MsgBox n & " Sheets Exported"
    
    End Sub