This article demonstrates macros that save worksheets to a single pdf file.
Exporting all worksheets in a workbook to PDF programmatically can be useful for several reasons:
By exporting all worksheets in a workbook to PDF programmatically, you can automate this process and take advantage of these benefits.
The following macro saves all worksheets in the active workbook to a single pdf file.
'Name macro Sub ExportWbtoPdf() 'Select all worksheets in active workbook For Each WS In ActiveWorkbook.Worksheets Worksheets(WS.Name).Select False Next WS 'Ask for a directory to save the pdf file in With Application.FileDialog(msoFileDialogFolderPicker) .Show myfolder = .SelectedItems(1) & "\" End With 'Ask for a save file name for the pdf file myfile = InputBox("Enter file name", "Save as..") 'Save all worksheets in workbook to pdf file ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ myfolder & myfile _ , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _ :=False, OpenAfterPublish:=True End Sub
After you have saved the above code to a code module you can start the macro by:
The macro now saves each worksheet in the workbook to the pdf file name you specified.
'Name macro Sub SaveSelectedSheetsToPDF() 'Dimension variables and declare data types Dim str As String, myfolder As String, myfile As String 'Get names from worksheets str = "Do you want to save these sheets to a single pdf file?" & Chr(10) For Each sht In ActiveWindow.SelectedSheets str = str & sht.Name & Chr(10) Next sht 'Display Message box answer = MsgBox(str, vbYesNo, "Continue with save?") If answer = vbNo Then Exit Sub 'Pick a directory With Application.FileDialog(msoFileDialogFolderPicker) .Show myfolder = .SelectedItems(1) & "\" End With 'Ask for folder myfile = InputBox("Enter filename", "Save as..") 'Save pdf file ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _ myfolder & myfile _ , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _ :=False, OpenAfterPublish:=True 'Quit macro End Sub
Make sure you select the worksheets you want to save as a single pdf file before you run the macro. To select multiple worksheets press and hold CTRL and then press with left mouse button on with the mouse on the worksheet tabs located at the bottom of your Excel screen.
This happens when you start the macro
If you save the macro in a personal macro workbook, you can access that macro no matter what workbook you have open.
Export-all-worksheets-in-workbook-to-PDF-programmaticallyv3.xlsm
Excel calendar
Table of Contents Excel monthly calendar - VBA Calendar Drop down lists Headers Calculating dates (formula) Conditional formatting Today Dates […]
Save invoice data – VBA
Table of contents Save invoice data - VBA Invoice template with dependent drop down lists Select and view invoice - […]
Working with COMBO BOXES [Form Controls]
This blog post demonstrates how to create, populate and change comboboxes (form control) programmatically. Form controls are not as flexible […]