Excel VBA export each sheet into one PDF page

Excel VBA export each sheet into one PDF page

To export each sheet in an Excel workbook to a separate PDF page, you can use VBA. This process involves saving each sheet individually as a PDF file. Below is a VBA macro that accomplishes this task:

VBA Macro to Export Each Sheet to a Separate PDF

Sub ExportSheetsToPDF() Dim ws As Worksheet Dim pdfFolderPath As String Dim pdfFileName As String Dim pdfFilePath As String ' Define the folder path where the PDFs will be saved pdfFolderPath = "C:\YourFolderPath\" ' Change to your desired folder path ' Check if the folder path exists If Dir(pdfFolderPath, vbDirectory) = "" Then MsgBox "The specified folder path does not exist. Please update the path in the code.", vbExclamation Exit Sub End If ' Loop through each worksheet in the workbook For Each ws In ThisWorkbook.Worksheets ' Define the PDF file name and path pdfFileName = ws.Name & ".pdf" pdfFilePath = pdfFolderPath & pdfFileName ' Export the sheet as a PDF ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfFilePath, Quality:=xlQualityStandard ' Notify user Debug.Print "Exported " & ws.Name & " to " & pdfFilePath Next ws ' Notify user when done MsgBox "All sheets have been exported as PDF files.", vbInformation End Sub 

Explanation:

  1. Define the Folder Path:

    • pdfFolderPath = "C:\YourFolderPath\": Change this to the folder where you want to save the PDF files. Ensure the folder exists; otherwise, you'll need to create it.
  2. Check Folder Existence:

    • If Dir(pdfFolderPath, vbDirectory) = "": Checks if the folder exists. If not, it shows a message and exits the macro.
  3. Loop Through Worksheets:

    • For Each ws In ThisWorkbook.Worksheets: Iterates through each worksheet in the workbook.
    • pdfFileName = ws.Name & ".pdf": Sets the PDF file name based on the worksheet name.
    • pdfFilePath = pdfFolderPath & pdfFileName: Combines the folder path and file name to get the full path.
  4. Export as PDF:

    • ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfFilePath, Quality:=xlQualityStandard: Exports the worksheet to a PDF file with standard quality.
  5. Notify User:

    • Debug.Print "Exported " & ws.Name & " to " & pdfFilePath: Prints a message in the Immediate Window for each exported sheet.
    • MsgBox "All sheets have been exported as PDF files.": Displays a message box when all sheets have been exported.

How to Use the Macro:

  1. Open the VBA Editor: Press Alt + F11.
  2. Insert a New Module: Go to Insert > Module.
  3. Paste the Code: Copy and paste the VBA code into the module.
  4. Close the VBA Editor: Press Ctrl + Q or close the editor window.
  5. Run the Macro: Press F5 or go to Run > Run Sub/UserForm.

Notes:

  • Folder Path: Ensure that the folder path specified in pdfFolderPath exists and is writable.
  • File Naming: The macro names each PDF file after the worksheet. Ensure worksheet names are valid for file naming.
  • PDF Quality: You can change Quality:=xlQualityStandard to Quality:=xlQualityMinimum if you prefer lower quality.

This VBA macro automates the process of exporting each worksheet as a separate PDF file, making it easy to manage and share individual sheets.

Examples

  1. How to export each sheet of an Excel workbook to a single PDF page using VBA?

    • Description: This VBA code exports each sheet in an Excel workbook to a separate page in a single PDF file.
    • Code:
      Sub ExportSheetsToPDF() Dim ws As Worksheet Dim pdfPath As String pdfPath = Application.GetSaveAsFilename(FileFilter:="PDF Files (*.pdf), *.pdf") If pdfPath = "False" Then Exit Sub ' Export all sheets to a single PDF file For Each ws In ThisWorkbook.Worksheets ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False Next ws End Sub 
  2. How to export each worksheet in Excel to a separate PDF page with VBA?

    • Description: This VBA script exports each worksheet in the workbook as a separate page in a single PDF.
    • Code:
      Sub ExportEachSheetToPDF() Dim ws As Worksheet Dim pdfPath As String pdfPath = Application.GetSaveAsFilename(FileFilter:="PDF Files (*.pdf), *.pdf") If pdfPath = "False" Then Exit Sub ' Export each sheet as a separate page in one PDF With ThisWorkbook .ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True End With End Sub 
  3. How to create a PDF from all sheets in Excel using VBA, with each sheet on one page?

    • Description: This VBA code exports all sheets from an Excel workbook to a single PDF file with each sheet on its own page.
    • Code:
      Sub CreatePDFFromAllSheets() Dim pdfPath As String pdfPath = Application.GetSaveAsFilename(FileFilter:="PDF Files (*.pdf), *.pdf") If pdfPath = "False" Then Exit Sub ' Export all sheets to one PDF file ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End Sub 
  4. How to export each Excel sheet to a single-page PDF using VBA?

    • Description: This VBA code ensures that each sheet in the workbook is sized to fit on one PDF page.
    • Code:
      Sub ExportSheetsSinglePagePDF() Dim ws As Worksheet Dim pdfPath As String pdfPath = Application.GetSaveAsFilename(FileFilter:="PDF Files (*.pdf), *.pdf") If pdfPath = "False" Then Exit Sub For Each ws In ThisWorkbook.Worksheets ws.PageSetup.Zoom = False ws.PageSetup.FitToPagesTall = 1 ws.PageSetup.FitToPagesWide = 1 Next ws ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End Sub 
  5. How to save each Excel worksheet as a page in a single PDF file using VBA?

    • Description: Use this VBA macro to save each worksheet as a page in a single PDF file.
    • Code:
      Sub SaveEachSheetAsPDFPage() Dim pdfPath As String pdfPath = Application.GetSaveAsFilename(FileFilter:="PDF Files (*.pdf), *.pdf") If pdfPath = "False" Then Exit Sub ' Export each worksheet to a single PDF file ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End Sub 
  6. How to convert each worksheet to a PDF page using VBA in Excel?

    • Description: This VBA script converts each worksheet into a PDF, ensuring each is on its own page.
    • Code:
      Sub ConvertSheetsToPDFPages() Dim ws As Worksheet Dim pdfPath As String pdfPath = Application.GetSaveAsFilename(FileFilter:="PDF Files (*.pdf), *.pdf") If pdfPath = "False" Then Exit Sub ' Set print area and page settings for each sheet For Each ws In ThisWorkbook.Worksheets ws.PageSetup.PrintArea = ws.UsedRange.Address Next ws ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End Sub 
  7. How to use VBA to export Excel sheets as single-page PDFs in one file?

    • Description: Export each sheet as a single-page PDF in one combined PDF file using this VBA code.
    • Code:
      Sub ExportSheetsAsSinglePagePDFs() Dim ws As Worksheet Dim pdfPath As String pdfPath = Application.GetSaveAsFilename(FileFilter:="PDF Files (*.pdf), *.pdf") If pdfPath = "False" Then Exit Sub ' Set each sheet to fit on one page and export For Each ws In ThisWorkbook.Worksheets ws.PageSetup.Zoom = False ws.PageSetup.FitToPagesWide = 1 ws.PageSetup.FitToPagesTall = 1 Next ws ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End Sub 
  8. How to export all worksheets to a single PDF file with each sheet on a new page using VBA?

    • Description: This VBA code exports all worksheets to a single PDF, with each worksheet on a new page.
    • Code:
      Sub ExportAllSheetsToPDF() Dim pdfPath As String pdfPath = Application.GetSaveAsFilename(FileFilter:="PDF Files (*.pdf), *.pdf") If pdfPath = "False" Then Exit Sub ' Export all worksheets in the workbook to one PDF ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End Sub 
  9. How to generate a PDF with each Excel sheet as a separate page using VBA?

    • Description: This code generates a PDF with each sheet in the workbook appearing as a separate page in the PDF.
    • Code:
      Sub GeneratePDFWithSheets() Dim pdfPath As String pdfPath = Application.GetSaveAsFilename(FileFilter:="PDF Files (*.pdf), *.pdf") If pdfPath = "False" Then Exit Sub ' Export all sheets to one PDF file ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End Sub 
  10. How to use VBA to export each worksheet as a single PDF page in Excel?

    • Description: This VBA macro ensures each worksheet is exported as a single page in a PDF file.
    • Code:
      Sub ExportWorksheetsToSinglePagePDF() Dim pdfPath As String pdfPath = Application.GetSaveAsFilename(FileFilter:="PDF Files (*.pdf), *.pdf") If pdfPath = "False" Then Exit Sub ' Configure each sheet to fit on one page For Each ws In ThisWorkbook.Worksheets ws.PageSetup.Zoom = False ws.PageSetup.FitToPagesWide = 1 ws.PageSetup.FitToPagesTall = 1 Next ws ' Export all sheets to a single PDF file ThisWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False End Sub 

More Tags

go-map elasticsearch-dsl stackexchange.redis spring-retry forward-declaration sbt operands math class-design .net-4.5

More Programming Questions

More Mixtures and solutions Calculators

More Entertainment Anecdotes Calculators

More Trees & Forestry Calculators

More Gardening and crops Calculators