Get our FREE VBA eBook of the 30 most useful Excel VBA macros.

Automate Excel so that you can save time and stop doing the jobs a trained monkey could do.

Claim your free eBook

How to save Excel as PDF with VBA (10 examples)

VBA Code Snippets

Since Excel 2010, it has been possible to save Excel as PDF. The PDF format was then and continues to be, one of the most common file formats for distributing documents.

The code examples below provide the VBA macros to automate the creation of PDFs from Excel using the ExportAsFixedFormat method. This means you do not require a PDF printer installed, as Excel can print directly to a PDF document.

The example codes can be used independently or as part of a larger automation process. For example, check out this post to see an example of how to loop through a list and print a PDF for each item: Create multiple PDFs based on a list

Rather than going from Excel to PDF, you might want to go the other way; from PDF to Excel. Check out these posts for possible solutions for that scenario:

Download the example file: Click the link below to download the example file used for this post:

Saving Excel workbooks, sheets, charts, and ranges as PDF

This section contains the base code to save Excel as PDF from different objects (workbooks, worksheets, ranges, and charts). From a VBA perspective, it is the ExportAsFilxedFormat method combined with the Type property set to xlTypePDF that creates a PDF.

Save active sheet as PDF

The following code saves the selected sheets as a single PDF.

Sub SaveActiveSheetsAsPDF()

'Create and assign variables
Dim saveLocation As String
saveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf"

'Save Active Sheet(s) as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _

End Sub

Save active workbook as PDF

Use the following macro to save all the visible sheets from a workbook.

Sub SaveActiveWorkbookAsPDF()

'Create and assign variables
Dim saveLocation As String
saveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf"

'Save active workbook as PDF
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _

End Sub

Save selection as PDF

Sometimes, we only want to save a small part of a worksheet to a PDF. The following code prints only the selected cells.

Sub SaveSelectionAsPDF()

'Create and assign variables
Dim saveLocation As String
saveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf"

'Save selection as PDF
Selection.ExportAsFixedFormat Type:=xlTypePDF, _

End Sub

Save a range as PDF

The macro below saves a specified range as a PDF.

Sub SaveRangeAsPDF()

'Create and assign variables
Dim saveLocation As String
Dim ws as Worksheet
Dim rng As Range

saveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf"
Set ws = Sheets("Sheet1")
Set rng = ws.Range("A1:H20")

'Save a range as PDF
rng.ExportAsFixedFormat Type:=xlTypePDF, _

End Sub

Save a chart as PDF

The VBA code below saves a specified chart as a PDF.

Sub SaveChartAsPDF()

'Create and assign variables
Dim saveLocation As String
Dim ws As Worksheet
Dim cht As Chart

saveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf"
Set ws = Sheets("Sheet1")
Set cht = ws.ChartObjects("Chart 1").Chart

'Save a chart as PDF
cht.ExportAsFixedFormat Type:=xlTypePDF, _

End Sub

Rather than naming a specific chart, the macro could run based on the active chart. Change this:

Set cht = ws.ChartObjects("Chart 1").Chart

To this:

Set cht = ActiveChart

Adapting the code to your scenario

To adapt the code examples to your specific needs, you should adjust certain lines of code.

Change the save location

To save the file in the correct location, change this list of code:

saveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf"

If you would prefer the save location to be included in a cell, change the code to reference the sheet and cell containing the file path.

saveLocation = Sheets("Sheet1").Range("B2").Value

Change the worksheet

In this line of code, change the text “Sheet1” to the sheet name in your workbook.

Set ws = Sheets("Sheet1")

Change the range

The following line of codes references the range to be printed to PDF.

Set rng = ws.Range("A1:H20")

Change the chart

To print a chart to PDF, change the chart’s name in the following line of code.

Set cht = ws.ChartObjects("Chart 1").Chart

If you are unsure of the chart name, it is shown in the Name box when the chart is selected.

Notes for saving PDF documents

While the Filename property is optional, it is important to know where the file is saved.

  • If the Filename property is not provided, the PDF saves in your default folder location using the Excel workbook’s name with the .pdf file extension.
  • Where a file name is provided, but not a file path, the document saves in your default folder location with the name provided.
  • When the .pdf file extension is not provided, the suffix is added automatically.
  • If a PDF exists in the specified save location, the existing file is overwritten. Therefore, it may be necessary to include file handling procedures to prevent overwriting existing documents and handling errors.
  • To save as an XPS document format, change xlTypePDF for xlTypeXPS.

Selecting specific worksheets before saving as PDF

If more than one worksheet is active, the PDF created includes all the active sheets. The following code selects multiple worksheets from an array before saving the PDF.

Sub SelectSheetsAndSaveAsPDF()

'Create and assign variables
Dim saveLocation As String
Dim sheetArray As Variant

saveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf"
sheetArray = Array("Sheet1", "Sheet2")

'Select specific sheets from workbook, the save all as PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _

End Sub

In the code above, an array is used to select the specific sheets. Alternatively, the Split array function with a text string could provide a more dynamic solution. This method is covered here: VBA Arrays.

Looping and saving as separate PDFs

To save multiple PDFs quickly, we can use VBA to loop through sheets or charts and save each individually.

Loop through sheets

The following macro loops through each worksheet in the active workbook and saves each as its own PDF. Each PDF is saved in the same folder as the workbook, where each PDF’s name is based on the worksheet’s name.

Sub LoopSheetsSaveAsPDF()

'Create variables
Dim ws As Worksheet

'Loop through all worksheets and save as individual PDF in same folder
'as the Excel file
For Each ws In ActiveWorkbook.Worksheets

    ws.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=ThisWorkbook.Path & "/" & ws.Name & ".pdf"


End Sub

Loop through selected sheets

The following macro loops through only the selected worksheets in the active workbook and saves each as its own PDF.

Sub LoopSelectedSheetsSaveAsPDF()

'Create variables
Dim ws As Worksheet
Dim sheetArray As Variant

'Capture the selected sheets
Set sheetArray = ActiveWindow.SelectedSheets

'Loop through each selected worksheet
For Each ws In sheetArray


    ws.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=ThisWorkbook.Path & "/" & ws.Name & ".pdf"

Next ws

'Reselect the selected sheets

End Sub

Loop through charts

The following code loops through each chart on the active sheet and saves each as a separate PDF.

Sub LoopChartsSaveAsPDF()

'Create and assign variables
Dim chtObj As ChartObject
Dim ws As Worksheet

Set ws = ActiveSheet

'Loop through all charts and save as individual PDF in same folder
'as the Excel file
For Each chtObj In ws.ChartObjects

    chtObj.Chart.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=ThisWorkbook.Path & "/" & chtObj.Name & ".pdf"

Next chtObj

End Sub

Other PDF print options

When using ExportAsFixedFormat, there are other optional settings available:

'Open the document after it is saved - options are True / False

'Include the Excel document properties into the PDF - options are True / False

'Does the created PDF adhere to the Print Areas already set in the 
'worksheet - options are True / False

'Set the output quality of the created document - options are 
'xlQualityMinimum / xlQualityStandard

'The page to start printing. If excluded, will start from the first page

'The page to print to. If excluded, will go to the last page

VBA Save to PDF Example using all the options

The code below demonstrates how to use all the options within a single macro. These options can be flexed to meet your requirements.

Sub SaveAsPDFOptions()

Dim saveLocation As String
saveLocation = "C:\Users\marks\Documents\myPDFFile.pdf"

'Example using all the options
ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=saveLocation, _
    OpenAfterPublish:=False, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    Quality:=xlQualityStandard, _
    From:=1, To:=2

End Sub

Other fixed formats available (xlTypeXPS)

The Type property can also create XPS documents when it is set to xlTypeXPS rather than xlTypePDF. XPS is Microsoft’s fixed file format; it is similar to PDF but based on the XML language. It is rarely used in the real world but is an option if required.


Learning how to save Excel as PDF is a good time investment. Each of these code snippets on its own is useful. However, the code examples above can be used in other automation to create even more time-saving.

Related posts:

Headshot Round

About the author

Hey, I’m Mark, and I run Excel Off The Grid.

My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn't until I was 35 that my journey really began.

In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).

Do you need help adapting this post to your needs?

I'm guessing the examples in this post don't exactly match your situation. We all use Excel differently, so it's impossible to write a post that will meet everybody's needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.

But, if you're still struggling you should:

  1. Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
  2. Ask the 'Excel Ninja' in your office. It's amazing what things other people know.
  3. Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it's clear and concise.  List all the things you've tried, and provide screenshots, code segments and example workbooks.
  4. Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.

What next?
Don't go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

2 thoughts on “How to save Excel as PDF with VBA (10 examples)

  1. Herby Schenker says:

    Hi Mark
    I like to save Excel Data from 7 different Worksheets into a single PDF File. Every Worksheets holds a Table on the same Position with the same number of Columns, but different numbers of rows.
    Ideal would be, when the export where able to merge the content on pages so that not every single Excel Table is on a separate Page.
    I would be infinitely grateful for any support, since I’ve been experimenting for days now

    • Excel Off The Grid says:

      Sorry, but Excel doesn’t let you do that natively. I think you would need to use VBA to create images of each range, then place those images below each other on a single sheet. When you print it will appear on a single page.

Leave a Reply

Your email address will not be published. Required fields are marked *