• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Microsoft Excel Tips and Tricks - Computergaga

  • Home
  • Excel Online Courses
  • Excel Functions List
  • Free Downloads
  • Blog
  • Tutorials
  • About
Home / VBA / Export Multiple Sheets to a Single PDF File – Excel VBA

Export Multiple Sheets to a Single PDF File – Excel VBA

This tutorial will demonstrate how to export multiple sheets of a workbook into a single PDF file using Excel VBA. The code can be seen below and adapted for your own use.

I have previously written a tutorial on exporting all the sheets of a workbook to PDF using VBA. This macro will allow you to be more specific as to what sheets are exported. It will also export to a single PDF file, rather than a separate file for each sheet.

Specifying the Sheets to be Exported

The key to the macro is to select the sheets that you want to export first. In the code below this is done with the following line.

Sheets(Array("Bradford", "Kettering")).Select

This references the sheets using their name. You could alternatively select the sheets using their index number like below.

Sheets(Array(2, 4, 5)).Select

Another alternative may be that the user selects the sheets in Excel, and then the macro is run. In this common scenario of only exporting the selected sheets, you would not need this line at all so it can be removed from the macro.

Exporting Specific Sheets to a Single PDF File – VBA Code

This VBA code can be copied and pasted into a module for the workbook. Change the code to work for your situation.

Sub ExportAsPDF()
Dim FolderPath As String
FolderPath = "C:\Users\Trainee1\Desktop\PDFs"
MkDir FolderPath
      
    Sheets(Array("Bradford", "Kettering")).Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FolderPath & "\Sales", _
        openafterpublish:=False, ignoreprintareas:=False
   
MsgBox "All PDF's have been successfully exported."
End Sub

If you were hoping to export sheets from multiple workbooks saved somewhere in your network, then check out this tutorial on looping through the files of a folder using VBA. By combining the code from the two macros you could export many PDF’s with one click of a button.

More awesome Excel VBA Tutorials

  • Loop Through the Subfolders of a Directory using FileSystemObject
  • Automatically Create a Table of Contents in Excel
  • Using the Offset Property in VBA

Related Posts:

  • N Functions in Excel thumbnail
    N Function in Excel
  • Copy a formula down in Excel
    How to Copy a Formula Down in Excel
  • SWITCH function feature
    Excel SWITCH Function - Nested IF Function Alternative
  • Case sensitive XLOOKUP feature
    Case Sensitive Lookup with XLOOKUP in Excel

Reader Interactions

Comments

  1. Nathan says

    26 October 2016 at 5:57 pm

    This is great – Thank you so much for showing me this. I have this exact situation – but I need to print Charts in separate excel sheets all into one pdf. I would like to have each chart fit to 1 page tall and 1 page wide regardless of where the chart is in each sheet and regardless of it’s size. Do you know how to do this? I’m new to all programming so any thoughts appreciated.
    Thanks again

    Reply
  2. Peter Allen says

    11 August 2017 at 2:49 pm

    Thanks for this Alan.
    This is terrific. I have to create sheets in a workbook for my boss.
    With this code I can print a PDF document with all the sheets on it.
    Many thanks
    Peter

    Reply
    • computergaga says

      12 August 2017 at 5:53 am

      Your welcome Peter, thanks.

      Reply
  3. georgia says

    30 September 2020 at 4:45 pm

    Hello.

    I am trying out his macro and it all works however i could like the file name to be linked t a call in the excel file e.g. it should link to cell C6 instead of “Sales” like your example above.

    Is there any way of doing this?

    Reply
    • Alan Murray says

      30 October 2020 at 7:22 am

      Hi Georgia,
      Sure, you can adapt that bit of code as follows.
      Filename:=FolderPath & “\” & Worksheets(“Kettering”).Range(“C6”)
      Alan

      Reply
  4. Francois Trout says

    17 December 2020 at 4:45 pm

    Super
    Can you export only one page (example all selected sheets second pages) to only one output PDF ?

    Reply
    • Alan Murray says

      23 December 2020 at 10:34 am

      I’m sure this would be possible using the print settings. It is not code that I have to hand though.

      Reply
  5. Gerardo Carrillo says

    23 December 2020 at 5:51 am

    I’m getting a Runtime error ‘9’ Subscript out of range for the

    ThisWorkbook.Sheets(Array(“Sum_instructed”, “Sum_pending”)).Select

    Reply
    • Alan Murray says

      23 December 2020 at 10:33 am

      Just check that the sheet names are correct, because that type of error indicates that it cannot find those sheets. Also, that the macro in the same workbook file that you are running it on. ThisWorkbook refers to the workbook that the macro resides in.

      Reply
  6. Peeter says

    10 February 2021 at 9:04 pm

    Hello,

    how to change the order in a pdf file, how worksheets are issued?
    I need the “introduction” to always be before “Sheet1”, but I always get the first page as “Sheet1” in the content of the pdf file.

    I tried both without luck:
    Spreadsheets (array (“introduction”, “page 1”)). Select
    Spreadsheets (array (“Sheet1”, “intro”)). Select

    How to change printout sequence ?

    Reply
  7. Allan Christensen says

    12 May 2021 at 8:39 am

    Hello
    Thank you for a very great video. I was just wondering, is it possible to get the possibility to type the name of the file, instead of it is written in the code….?? “Filename:=FolderPath & “\Sales”

    Best regards
    Allan

    Reply
    • Alan Murray says

      19 May 2021 at 6:42 am

      Sure, the name could be picked up from a range on the sheet or maybe have an input box prompt you if you prefer.

      Reply
  8. tran says

    7 July 2021 at 4:21 am

    Hi Allan,

    Could you please modify the code to have it pop up for the location to save the file?
    Thanks.

    Reply
    • Alan Murray says

      26 July 2021 at 9:08 pm

      This can be done using application.FileDialog(msoFileDialogFolderPicker).
      I show an example of using this in my VBA course – http://bit.ly/37XSKfZ

      Reply
  9. Amit kumar says

    7 January 2023 at 2:14 pm

    how can i do print using single assigned button for all pdf using vba code

    Reply
  10. Aaron Reasoner says

    24 May 2023 at 2:30 pm

    Nice example! If I wanted to reference specific “named ranges” on multiple sheets, what adjustments should be made? For context; if sheet “Bradford” had 2 separate recaps (NamedRange_1, NamedRange_2) and sheet “Kettering” also had 2 separate recaps (NamedRange_3, NamedRange_4), how do I get “NamedRange_2” and “NamedRange_4” to save into 1 PDF file?

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

Recent Posts

  • Excel SWITCH Function – Nested IF Function Alternative
  • Sum Formulas Only in Excel
  • How to Calculate Age in Excel
  • Custom Sort in Excel
  • Case Sensitive Lookup with XLOOKUP in Excel
Advanced Excel Success book

Footer

Contact

[email protected]

LinkedIn Twitter YouTube

Useful Links

  • Home
  • Excel Online Courses
  • Tutorials
  • Sitemap
  • Privacy Policy
  • Jobs by Jooble

Course Topics

  • Blog
  • Charts
  • Courses
  • Dashboards
  • Downloads
  • Excel Tips
  • Formulas
  • Mail Merge
  • Office
  • PivotTables
  • Power BI
  • Power Pivot
  • Power Query
  • VBA

Join Our Mailing List

© Copyright 2009 Computergaga · All Rights Reserved ·