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.
- 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
- To see the pdf, go to the file location that you put into the VBA code for saving exported pdf from Excel.
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.
- Click on Developer >> Visual Basic >> Insert >> Module. A module window will pop up.
- 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
- You can see the saved location and name of exported pdf file from Excel.
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.
- 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
- 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.
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.
- 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
- The pdf files are at the same location as the Excel file.
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.
- 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
- The pdf will be in the location entered into the VBA code at saveLocation..
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.
- 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
- The pdf file will be in the location that you put in the code.
Download Practice Workbook