Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

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.

Use Command Button to save active worksheet as PDF file with VBA code

Save a range of selected cells as PDF file with Kutools for Excel

Save each worksheet as individual PDF file of active workbook with Kutools for Excel


Easily save selection as PDF file in Excel:

Click Enterprise > Import/Export > Export Range to File. The Kutools for Excel's Export Range to File utility helps you easily export selection as PDF file in Excel. See below screenshot:

Kutools for Excel includes more than 300 handy Excel tools. Free to try with no limitation in 30 days. Download the free trial Now

doc selection to pdf


Use Command Button to save active worksheet as PDF file with VBA code


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.


Save a range of selected cells as PDF file with Kutools for Excel

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.

Tip.If you want to have a free trial of this utility, please go to download the software freely first, and then go to apply the operation according above steps.


Save each worksheet as individual PDF file of active workbook with Kutools for Excel

You can use the Split Workbook utility of Kutools for Excel to save each worksheet in active workbook as individual PDF file easily.

Kutools for Excel : with more than 300 handy Excel add-ins, free to try with no limitation in 60 days.

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.

Tip.If you want to have a free trial of this utility, please go to download the software freely first, and then go to apply the operation according above steps.


Demo: Save selection or each worksheet as PDF with Kutools for Excel

Kutools for Excel includes more than 300 handy Excel tools. Free to try with no limitation in 60 days. Download the free trial now!


Related articles:


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Nzee · 5 months ago
    IT SHOWS Run-Time error '1004'
    • To post as a guest, your comment is unpublished.
      crystal · 4 months ago
      Hi Nzee,
      I need to know your Excel version. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Randy · 9 months ago
    I 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.
    Jo Colenso · 1 years ago
    I 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.
      crystal · 1 years ago
      Good 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", _
      OpenAfterPublish:=False
      Application.ScreenUpdating = True
      End Sub
  • To post as a guest, your comment is unpublished.
    Ian · 2 years ago
    Hi,
    How would one allow the user to enter the file name as opposed to "Export.pdf"?
    Thanks,
    Ian
    • To post as a guest, your comment is unpublished.
      Mayur · 1 years ago
      Sub SaveAsPDF()
      Application.Screen Updating = False
      ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
      Filename:=Application.InputBox("Enter File Name")

      strExcelPath = "H:\My Documents"

      Application.ScreenUpdating = True
      End Sub