Excel Macro to Save as PDF (5 Suitable Examples)

Example 1 – Creating Excel Macro to Save Active Workbook as PDF

Steps:

  • Select the sheets to save as pdf. You can select all the sheets by right-clicking on any sheet name and choosing Select All Sheets.
  • Right-click on any sheet name and click on View Code. A module window will pop up.

Saving Active Workbook as PDF

  • Enter the following macro. Modify the saveLocation part of the code with your own pdf file save location. Click on the Run button.
Sub SavingActiveWorkbookAsPDF()
Dim saveLocation As String
saveLocation = "C:\Users\sohan\Desktop\Softeko\57-0040\PDF files\mypdf2.pdf"
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation
End Sub

Macro to Save Active Workbook as PDF

  • To see the pdf, go to the file location that you put into the VBA code for saving exported pdf from Excel.

Saving Active Workbook as PDF

Read More: Excel VBA Macro to Save PDF in Specific Folder


Example 2 – Using Macro to Save Active Worksheet as PDF

Steps:

  • Go to the sheet that you need to save as a pdf.

Saving Active Worksheet as PDF

  • Click on Developer >> Visual Basic >> Insert >> Module. A module window will pop up.

Saving Active Worksheet as PDF

  • Enter the VBA code below. Click on the Run You need to edit the saveLocation in the VBA code with your own file directory address where you want to save the exported pdf.

In this example, C:\Users\sohan\Desktop\Softeko\57-0040\PDF files is the file address to save the exported pdf files. And myPDF1 is the pdf file’s name.

Sub SavingActiveSheetsAsPDF()
Dim saveLocation As String
saveLocation = "C:\Users\sohan\Desktop\Softeko\57-0040\PDF files\myPDF1.pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation
End Sub

Macro to Save Active Worksheet as PDF

  • You can see the saved location and name of exported pdf file from Excel.

Saving Active Worksheet as PDF

Read More: Excel Macro to Save as PDF with Filename from Cell Value


Example 3 – Saving Excel Sheets in Separate PDF Files with Macro

3.1 Saving All Sheets as PDF

Steps:

  • Open an active workbook. Right-click on a sheet name and click on View Code. A new module window will pop up.

Using Loop through Sheets

  • Copy the following VBA code and paste it into the module window. Click on the Run button.
Sub LoopSheetsSaveAsPDF()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "/" & ws.Name & ".pdf"
Next
End Sub

Macro to Save Active Worksheet as PDF

  • Each sheet is saved as its own pdf file. And the files will be saved in the same location where your Excel file is located.

Using Loop through Sheets in Excel Macro to Save as PDF


3.2 Saving Selected Sheets as PDF

Steps:

  • Select the sheets one after the other by pressing Right-click on any selected sheet name and click on View Code. A new module window will pop up.

Using Loop through Selected Sheets in Excel Macro to Save as PDF

  • Copy the following VBA code and paste it into the module.
Sub SelectedSheetsSaveAsPDFByLoop()
Dim ws As Worksheet
Dim sheets_Array As Variant
Set sheets_Array = ActiveWindow.SelectedSheets
For Each ws In sheets_Array
ws.Select
ws.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=ThisWorkbook.Path & "/" & ws.Name & ".pdf"
Next ws
sheets_Array.Select
End Sub

Macro to Save Active Worksheet as PDF

  • The pdf files are at the same location as the Excel file.

Using Loop through Selected Sheets in Excel Macro to Save as PDF


Example 4 – Saving a Particular Selection from Excel Sheet as PDF

Steps:

  • Select the section that you want to save as a pdf. Right-click on the sheet name and click on View Code. A new module window will pop up.

Saving Selection as PDF

  • Enter the code below. Click Run.
Sub SavingSelectionAsPDF()
Dim FileLocation As String
FileLocation = "C:\Users\sohan\Desktop\Softeko\57-0040\PDF files\mypdf3.pdf"
Selection.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=FileLocation
End Sub

Saving Selection as PDF with Macro

  • The pdf will be in the location entered into the VBA code at saveLocation..

Saving Selection as PDF

Read More: Excel VBA: Choose Location and Save as PDF


Example 5 – Saving a Specific Range in Excel as PDF

Steps:

  • Go to the sheet containing the range you need to save as a pdf.

Saving a Range as PDF

  • Go to the module window to enter the following VBA code modifying with your own save location. Click on the Run You can open a new module window either a right-click on the sheet name and clicking on the View Code or go to Developer >> Visual Basic >> Insert >> Module.
Sub SaveRangeAsPDF()
Dim saveLocation As String
Dim rng As Range
saveLocation = "C:\Users\sohan\Desktop\Softeko\57-0040\PDF files\mypdf4.pdf"
Set rng = Sheets("Sheet1").Range("B5:F12")
rng.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=saveLocation
End Sub

Saving a Range as PDF with Macro

  • The pdf file will be in the location that you put in the code.

Saving a Range as PDF


Download Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Hafizul Islam
Hafizul Islam

Hafizul Islam is an excellent marine engineer who loves working with Excel and diving into VBA programming. For him, programming is like a superhero tool that saves time when dealing with data, files, and the internet. His skills go beyond the basics, including Rhino3D, Maxsurf C++, AutoCAD, Deep Neural Networks, and Machine Learning. He got his B.Sc in Naval Architecture & Marine Engineering from BUET, and now he's switched gears, working as a content developer. In this role,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo