We have an Excel workbook consisting of three worksheets. The first worksheet is Sheet 1, and the data is shown in the picture below. The second worksheet is Sheet 2, and the data in this sheet is shown in the following picture. The third worksheet is Sheet 3, and the data in this sheet is shown in the picture below. We will learn to print all these Excel sheets into a PDF file with VBA.
Sub PrintAllSheetToPdf() For Each iSheet In ActiveWorkbook.Worksheets Worksheets(iSheet.Name).Select False Next iSheet With Application.FileDialog(msoFileDialogFolderPicker) .Show iFolder = .SelectedItems(1) & "\" End With iFile = InputBox("Enter New File Name", "PDF File Name") ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=iFolder & iFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True End Sub
Your code is now ready to run.
All the sheets in your workbook are automatically selected (notice in the image below).
Your newly created PDF file from multiple Excel sheets will start printing. This is how you print all the sheets from the Excel workbook into a single PDF file with VBA.
VBA Code Explanation
Sub PrintAllSheetToPdf()
Name the sub-procedure of the macro.
For Each iSheet In ActiveWorkbook.Worksheets Worksheets(iSheet.Name).Select False Next iSheet
Select all worksheets in the active workbook.
With Application.FileDialog(msoFileDialogFolderPicker) .Show iFolder = .SelectedItems(1) & "\" End With
Ask for a directory to save the newly created PDF file in.
iFile = InputBox("Enter New File Name", "PDF File Name")
Ask for a name for the newly created PDF file.
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=iFolder & iFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Save all worksheets from the active workbook as a single PDF file –
End Sub
Ends the sub-procedure of the macro.
Steps:
Sub PrintActiveSheetToPdf() Dim msg As String Dim iFolder As String Dim iFile As String msg = "Do you want to save these worksheets to a single pdf file?" & amp & nbsp & Chr(10) For Each iSheet In ActiveWindow.SelectedSheets msg = msg & iSheet.Name & Chr(10) Next iSheet iText = MsgBox(msg, vbYesNo, "Confirm to Save as PDF. ") If iText = vbNo Then Exit Sub With Application.FileDialog(msoFileDialogFolderPicker) .Show iFolder = .SelectedItems(1) & "\" End With iFile = InputBox("Enter New File Name", "PDF File Name") ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=iFolder & iFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True End Sub
We have the newly created “Student Information (Selection)” PDF file in the “ExcelDemy” folder in Drive C, as shown below.
Print the PDF file:
Your newly created PDF file from multiple Excel sheets will start printing. This is how you print all the sheets by selection from the Excel workbook into a single PDF file with VBA.
VBA Code Explanation
Sub PrintActiveSheetToPdf()
Name the sub-procedure of the macro.
Dim msg As String Dim iFolder As String Dim iFile As String
Declare the variables.
msg = "Do you want to save these worksheets to a single pdf file?" & amp & nbsp & Chr(10) For Each iSheet In ActiveWindow.SelectedSheets msg = msg & iSheet.Name & Chr(10) Next iSheet
Select only the selected worksheets in the active workbook. Show the sheet names concatenated with a question in a MsgBox to confirm. Chr(10) is a carriage return.
iText = MsgBox(msg, vbYesNo, "Confirm to Save as PDF. ") If iText = vbNo Then Exit Sub
Execute the task according to Yes or No confirmation. If Yes, then continue with the procedure. If No, then exit the procedure.
With Application.FileDialog(msoFileDialogFolderPicker) .Show iFolder = .SelectedItems(1) & "\" End With
Ask for a directory to save the newly created PDF file in.
iFile = InputBox("Enter New File Name", "PDF File Name")
Ask for a name for the newly created PDF file.
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=iFolder & iFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Save all worksheets from the active workbook as a single PDF file –
End Sub
Ends the sub-procedure of the macro.
Steps:
Sub PrintSpecificSheetsToPdf() Dim iSheets As Variant iSheets = Array("Sheet1", "Sheet2") ThisWorkbook.Sheets(iSheets).PrintOut End Sub Sub PrintSpecificSheetsToPdfWithLoop() Dim iSheets() As String Dim iCount As Long ReDim iSheets(1 To ThisWorkbook.Sheets.Count) For iCount = LBound(iSheets) To UBound(iSheets) iSheets(iCount) = ThisWorkbook.Sheets(iCount).Name Next iCount ThisWorkbook.Sheets(iSheets).PrintOut End Sub
Your code is now ready to run.
See the indicated part in the following image. In that line of code, we hardcoded only the specified sheet name – Sheet1 and Sheet2 – that we want to export into PDF.
Now let’s check whether the “ExcelDemy” folder that we selected as the storage of our PDF file.
To print the PDF file:
Your newly created PDF file from multiple specified Excel sheets will start printing. This is how you print the multiple specified sheets from the Excel workbook into a PDF file with VBA.
VBA Code Explanation
Sub PrintSpecificSheetsToPdf()
Name the sub-procedure of the macro.
Dim iSheets As Variant
Declare the variable to store the multiple sheets.
iSheets = Array("Sheet1", "Sheet2")
Store the sheets that will be exported in the declared array variable.
ThisWorkbook.Sheets(iSheets).PrintOut
Prints the group of sheets with the PrintOut function .
End Sub
Ends the sub-procedure of the macro.
Sub PrintSpecificSheetsToPdfWithLoop()
Name the sub-procedure of the macro that will perform the loop operation.
Dim iSheets() As String Dim iCount As Long
Declare the variables .
ReDim iSheets(1 To ThisWorkbook.Sheets.Count)
Re-declare the array variable. This time it stores the number of the total sheet count in it.
For iCount = LBound(iSheets) To UBound(iSheets) iSheets(iCount) = ThisWorkbook.Sheets(iCount).Name Next iCount
This part of the code starts iterating from the smallest subscript to the largest subscript of the array and stores the array value in the variable. It continues doing this until it finishes scanning through the whole array.
ThisWorkbook.Sheets(iSheets).PrintOut
Prints the group of sheets with the PrintOut function.
End Sub
Ends the sub-procedure of the macro.
Steps:
Option Explicit Public Sub PrintSpecificSheetsToPdfWithRename() Dim iSheetList As Variant Dim iSheet As Worksheet Dim iFileName As String Dim iFilePath As String Set iSheet = ThisWorkbook.Sheets("Sheet1") iSheetList = Array("Sheet1", "Sheet2") iFilePath = "C:\ExcelDemy\" With iSheet iFileName = iFilePath & .Range("B5").Value & " " & .Range("C5").Value & "-" & .Range("D5").Value & ".pdf" End With ThisWorkbook.Sheets(iSheetList).Select iSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=iFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True iSheet.Select End Sub
Before running the code, do you see the indicated part in the following image?
Consider the following image to understand the naming system for the PDF file.
In this part of the code, we provided three cell numbers, Cell B5, C5, and D5 from Sheet1. Cell B5 has the value “John Cena,” Cell C5 has the value “101,” and Cell D5 has the value “89.” So, the name for the newly created PDF file will be “John Cena 101-89.” We also provided a hyphen (—) between Cell C5 and D5 in the code.
Now let’s check whether the “ExcelDemy” folder that we hardcoded as the storage of our PDF file.
To print the PDF file:
Your newly created PDF file from multiple hardcoded Excel sheets will start printing. This is how you print the multiple specified sheets from the Excel workbook into a single PDF file and name it according to the cell values from a sheet with VBA.
VBA Code Explanation
Option Explicit
Forces to declare all the variables explicitly of the file.
Public Sub PrintSpecificSheetsToPdfWithRename()
Name the sub-procedure of the macro.
Dim iSheetList As Variant Dim iSheet As Worksheet Dim iFileName As String Dim iFilePath As String
Declares all the variables .
Set iSheet = ThisWorkbook.Sheets("Sheet1")
Stores the sheet from where we will be taking the cell values to name our PDF file.
iSheetList = Array("Sheet1", "Sheet2")
Declares the sheets that will be exported into PDF.
iFilePath = "C:\ExcelDemy\"
Declares the directory to store our PDF file.
With iSheet iFileName = iFilePath & .Range("B5").Value & " " & .Range("C5").Value & "-" & .Range("D5").Value & ".pdf" End With
Selects the sheet and takes values from Cell B5, C5 and D5 to name the PDF file. Then exit the sheet.
ThisWorkbook.Sheets(iSheetList).Select iSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=iFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True iSheet.Select
Save the hardcoded worksheets from the active workbook as a single PDF file –
End Sub
Ends the sub-procedure of the macro.
Steps:
Sub PrintSheetsToPdfInFolder() Dim iFolderAdrs As String iFolderAdrs = "C:\ExcelDemy\New Student Information" MkDir iFolderAdrs Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=iFolderAdrs & "\Student Information", OpenAfterPublish:=False, IgnorePrintAreas:=False MsgBox "All worksheets are successfully exported into a single pdf!" End Sub
Your code is now ready to run.
Before proceeding, let’s describe a couple of things first. Can you see the part of the code indicated in the image below?
In that line of the code, we did the tricks. We store the path – C:\ExcelDemy – where we want to save the PDF file, and following that, we provide a name – “New Student Information” – as the new folder name in our case. You must write the path address according to your data, and you can provide any name that you want as your new folder name.
Consider the following image.
Now that you have understood the code’s key aspects, let’s check whether the newly created folder we hardcoded as “New Student Information” in the C: ExcelDemy path has been created.
To print the PDF file:
Your newly created PDF file from multiple hardcoded Excel sheets inside the new folder will start printing. This is how you print the multiple sheets from the Excel workbook into a single PDF file and store it inside a folder that you create with VBA.
VBA Code Explanation
Sub PrintSheetsToPdfInFolder()
Firstly, provide a name for the sub-procedure of the macro.
Dim iFolderAdrs As String
Declare the variable to carry the path address.
iFolderAdrs = "C:\ExcelDemy\New Student Information"
Stores the directory and declares a new folder name.
MkDir iFolderAdrs
Creates the new folder with Excel’s MkDir statement .
Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Select ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=iFolderAdrs & "\Student Information", OpenAfterPublish:=False, IgnorePrintAreas:=False
Selects the sheets and save them as a single PDF file –
MsgBox "All worksheets are successfully exported into a single pdf!"
Confirmation message after successfully file creation.
End Sub
Ends the sub-procedure of the macro.
Steps:
Sub PrintSpecificSheetsToPdfInCurrentPath() Dim iSheet As Worksheet Dim iBook As Workbook Dim iFileName As String Dim iFilePath As String Dim iFile As String Dim iPathFile As String Dim NewFile As Variant Dim msg As Long On Error GoTo errHandler Set iBook = ActiveWorkbook Set iSheet = ActiveSheet iFilePath = iBook.Path If iFilePath = "" Then iFilePath = Application.DefaultFilePath End If iFilePath = iFilePath & "\" iFileName = iSheet.Range("B6").Value & " - " & iSheet.Range("C6").Value & " - " & iSheet.Range("D6").Value iFile = iFileName & ".pdf" iPathFile = iFilePath & iFile If iOldFile(iPathFile) Then msg = MsgBox("Replace current file?", vbQuestion + vbYesNo, "Existing File!") If msg <> vbYes Then NewFile = Application.GetSaveAsFilename(InitialFileName:=iPathFile, FileFilter:="PDF Files (*.pdf), *.pdf", Title:="Enter folder and filename to save") If NewFile <> "False" Then iPathFile = NewFile Else GoTo exitHandler End If End If End If iSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=iPathFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False MsgBox "New PDF file is created: " & vbCrLf & iPathFile exitHandler: Exit Sub errHandler: MsgBox "There is an error while creating PDF file!" Resume exitHandler End Sub Function iOldFile(rsFullPath As String) As Boolean iOldFile = CBool(Len(Dir$(rsFullPath)) > 0) End Function
Your code is now ready to run.
To understand how we got the name and the file path, look at the following image.
See the indicated part in the above image. This did the trick of naming the file.
In this part of the code, we provided three cell numbers—Cell B6, C6, and D6—from the active sheet, Sheet1. Cell B6 has the value “Roman Reigns,” Cell C6 has the value “102,” and Cell D6 has the value “56.” So, the name for the newly created PDF file is “Roman Reigns—102 – 56.” We also provided a hyphen (—) between Cell B6, C6, and D6 in the code.
The path we got is none other than the path where we have the source Excel workbook. We have our workbook in the C:\ExcelDemy path, so the newly created PDF file “Roman Reigns – 102 – 56” is also stored in the exact same address.
Open the file, and you will see that the PDF has only one page, and the data on that page is the exact data from the active sheet, Sheet1, from the workbook.
To print the PDF file:
If you need to modify the Excel sheet and re-print it again, or if you want to keep both the old PDF file and the new PDF file:
Now go back to the file directory to check whether there are multiple copies of the same PDF file with different names.
This is how you print an active Excel sheet to a PDF and rename it multiple times with VBA.
VBA Code Explanation
Sub PrintSpecificSheetsToPdfInCurrentPath()
At first, name the sub-procedure of the macro.
Dim iSheet As Worksheet Dim iBook As Workbook Dim iFileName As String Dim iFilePath As String Dim iFile As String Dim iPathFile As String Dim NewFile As Variant Dim msg As Long
Then, declare the variables .
On Error GoTo errHandler
If any error occurs, then go to errHandler statement.
Set iBook = ActiveWorkbook Set iSheet = ActiveSheet
Later, store the active workbook and worksheet in the variables .
iFilePath = iBook.Path If iFilePath = "" Then iFilePath = Application.DefaultFilePath End If iFilePath = iFilePath & "\"
Gets the active workbook folder if the file is saved.
iFileName = iSheet.Range("B6").Value & " - " & iSheet.Range("C6").Value & " - " & iSheet.Range("D6").Value
After that, it takes values from Cell B6 , C6 and D6 to name the PDF file and store the name in the variable .
iFile = iFileName & ".pdf" iPathFile = iFilePath & iFile
creates a default name for the new file to save.
If iOldFile(iPathFile) Then msg = MsgBox("Replace current file?", vbQuestion + vbYesNo, "Existing File!") If msg <> vbYes Then NewFile = Application.GetSaveAsFilename(InitialFileName:=iPathFile, FileFilter:="PDF Files (*.pdf), *.pdf", Title:="Enter folder and filename to save") If NewFile <> "False" Then iPathFile = NewFile
It performs the file replacement operation. If the file already exists, then save the file with a new path and name. If the file doesn’t exist, then save the file in the current directory.
Else GoTo exitHandler End If End If End If
Otherwise, go to the exitHandler statement in the code.
iSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=iPathFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
Save the worksheet from the active workbook as a single PDF file –
MsgBox "New PDF file is created: " & vbCrLf & iPathFile
Confirmation message after successfully file creation.
exitHandler: Exit Sub
Code in the exitHandler statement to exit the sub-procedure.
errHandler: MsgBox "There is an error while creating PDF file!" Resume exitHandler
Code in the errHandler statement to notify that there is an error. Go back to the exitHandler statement.
End Sub
Finally, ends the sub-procedure of the macro.
Function iOldFile(rsFullPath As String) As Boolean iOldFile = CBool(Len(Dir$(rsFullPath)) > 0) End Function
Lastly, calls the function to execute all the operations that we just explained.
Download the Workbook
You can download the free practice Excel workbook from here.
Print Multiple Sheets to Single PDF.xlsm