How to use Command Button to save active worksheet as PDF file in Excel?
While working in Microsoft Excel, you may face the problem of saving active worksheet as PDF file. In this article, you can learn how to save active worksheet as PDF file with VBA code through a Command Button. And if you also want to save a range or each worksheet in active workbook as individual PDF file, this article can also help you.
Recommended Productivity Tools for Excel
Office Tab: Bring powerful tabs to Office (include Excel), just like Chrome, Safari, Firefox and Internet Explorer. Save you half the time, and reduce thousands of mouse clicks for you. 30-day Unlimited Free Trial
Kutools for Excel: Save 71% of your time and solve 82% Excel problems for you. 300+ advanced tools designed for 1500+ work scenario, make Excel much easy and increase productivity immediately.60-day Unlimited Free Trial
You can run the following VBA code to save an active worksheet as PDF file by clicking on a Command Button. Please do as follows.
1. Firstly, you need to create a folder named PDF for saving your new converted PDF file. Here I create this folder in my local disc (C:).
2. Please insert a Command Button by clicking Developer > Insert > Command Button (ActiveX Control). See screenshot:
2. Then draw a Command Button in to the worksheet you need to add new rows, right click the Command Button and click Properties from the right-clicking menu.
3. In the Properties dialog box, enter the displayed text of the Command Button into the Caption box under the Categorized tab, and then close the dialog box.
You can see the displayed text of the Command Button is changed as below screenshot shown.
4. Right click the Command Button again, and then click View Code from the right-clicking menu.
5. In the opening Microsoft Visual Basic for Applications window, please replace the original code in the Code window with the following VBA code.
VBA code: Command Button to save active worksheet as PDF
Private Sub CommandButton1_Click() Application.ScreenUpdating = False ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _ Filename:="C:\PDF\Export.pdf", _ OpenAfterPublish:=False Application.ScreenUpdating = True End Sub
Note: In the code, CommandButton1 is the Command Button name you will use to save active sheet as PDF file. "C:\PDF\Export.pdf" is the path and name of your saving PDF file.
6. Press the Alt + Q keys simultaneously to close the Microsoft Visual Basic for Applications window. Then turn off the Design Mode under the Developer tab.
Now, click on the Command Button, the active worksheet will be saved as a PDF file named Export and located in the specified location.
If you just want to save a range of selected cells as a PDF file, you can try the Export Range to File utility of Kutools for Excel. Please do as follows.
1. Select the range you need to save as PDF file, then click Enterprise > Import / Export > Export Range to File. See screenshot:
2. In the Export Range to File dialog box, you need to:
2.1 Select the PDF option in the File format section;
2.2 Specify a folder to save your PDF file in the Save direction section;
2.3 Click the OK button.
2.4 Name your PDF file in the second Export Range to File dialog box, and then click the OK button. See screenshot:
Note: You can check any of the Preserve formatting, Preserve row heights as well as Preserve column widths in the Workbook options section as you need. And you can open the exported PDF file automatically by checking the Open the file after exporting box.
Now the selected range is saved as PDF file.
You can use the Split Workbook utility of Kutools for Excel to save each worksheet in active workbook as individual PDF file easily.
1. In the workbook you need to save each worksheet as individual PDF file, then click Enterprise > Workbook > Split Workbook. See screenshot:
2. In the Split Workbook dialog box, you can see all worksheets are listed inside, please select PDF (*.pdf) option from the Save as type drop-down list, and then click the Split button. And in the popping up Browse For Folder dialog box, specify a folder where you want to save the PDF files, and then click the OK button. See screenshot:
Then all worksheets in active workbook are saved as individual PDF file immediately.
Kutools for Excel includes more than 300 handy Excel tools. Free to try with no limitation in 60 days. Download the free trial now!
- How to insert a blank new row automatically by Command Button in Excel?
- How to format cell to look like a 3D button in Excel?
- How to hide or unhide a Command Button based on specified cell value in Excel?
Recommended Productivity Tools
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
To post as a guest, your comment is unpublished.
To post as a guest, your comment is unpublished.· 9 months agoI can convert my files to PDF now with a command button, but can I also get that same function to allow a different name to each file; based on an existed number structure?
To post as a guest, your comment is unpublished.· 9 months agoHi Randy,
Sorry I can’t help with this, welcome to post any question in our forum: https://www.extendoffice.com/forum.html to get more Excel supports from our professional.
To post as a guest, your comment is unpublished.· 1 years agoI have a similar request I want the file to be named the same as a particular cell in the tab , IE: G19 ( This is the invoice number and I want the file to save as PDF with that name) ? . I have managed to get the export.pdf working but this isn't going to work on multiple saves.Any thoughts thanks
To post as a guest, your comment is unpublished.· 1 years agoGood Day,
The following VBA code can help you solve the problem. Please select cell G19 in the popping up Kutoos for Excel dialog box after running the code. Thank you for your comment.
Private Sub CommandButton1_Click()
Dim xRg As Range
Dim xName As String
On Error Resume Next
Application.ScreenUpdating = False
Set xRg = Application.InputBox("Select the cell you will name the PDF with the cell value:", "Kutools for Excel", Selection.Address, , , , , 8)
If xRg Is Nothing Then Exit Sub
xName = xRg(1).Value
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\PDF\" & xName & ".pdf", _
Application.ScreenUpdating = True
To post as a guest, your comment is unpublished.· 2 years agoHi,
How would one allow the user to enter the file name as opposed to "Export.pdf"?