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

Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier...
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
  • Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
  • More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
  • Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
  • Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
  • Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
  • Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
  • More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.

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:


Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
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.
    Sulaiman · 10 days ago
    How do I do this for MS Word? I have a bunch of questions with radio button options as answers. So I want to create a command button to output the questions with the selected answers only on another word document. How do I do this? A code would really help! I'll email you the document if you need it. Thanks!
  • To post as a guest, your comment is unpublished.
    atul tailor · 6 months ago
    plz paste coding for save as pdf button in microsoft excel
  • To post as a guest, your comment is unpublished.
    Nzee · 11 months ago
    IT SHOWS Run-Time error '1004'
    • To post as a guest, your comment is unpublished.
      crystal · 11 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 · 1 years 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