How to Convert Excel to PDF using VBA Code

Documents are preferred when they are in PDF format than when in other formats. Do you know you can convert your Excel document to PDF format? VBA tool allows users to convert Excel documents to PDF formats. This increases the compatibility of the document with other editors than when it is in Excel format. In addition, it is easier to secure your dataset in PDF than when it is in Excel format. Therefore, we shall discuss ways of converting Excel documents to PDF.

Converting a selection of Excel documents to PDF

This is where the user converts some parts of the Excel document to PDF. Here are the steps to do so:

1. Open the worksheet with the section you want to convert to PDF. Highlight the section you want to convert to PDF.

2. Click on the Developer tab on the Ribbon, and then locate the Visual Basic button.

3. In the Visual Basic screen, click the Insert tab on the Ribbon and select the Module button.

4. Type the following code in the empty module.

Sub PrintSelectionToPDF()

'SUBROUTINE: PrintSelectionToPDF

'DEVELOPER: Ryan Wells

'DESCRIPTION: Print your currently selected range to a PDF

Dim ThisRng As Range

Dim strfile As String

Dim myfile As Variant

If Selection.Count = 1 Then

Set ThisRng = Application.InputBox("Select a range", "Get Range", Type:=8)

Else

Set ThisRng = Selection

End If

'Prompt for save location

strfile = "Selection" & "_" _

& Format(Now(), "yyyymmdd_hhmmss") _

& ".pdf"

strfile = ThisWorkbook.Path & "\" & strfile

myfile = Application.GetSaveAsFilename _

(InitialFileName:=strfile, _

FileFilter:="PDF Files (*.pdf), *.pdf", _

Title:="Select Folder and File Name to Save as PDF")

If myfile <> "False" Then 'save as PDF

ThisRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _

myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _

IgnorePrintAreas:=False, OpenAfterPublish:=True

Else

MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File Selected"

End If

End Sub

5. Press the F5 button key to run the code. You'll be prompted to save the PDF file created.

To Convert All Sheets in the Workbook to PDF

Steps:

1. Open the workbook you want to convert to PDF.

2. Click on the Developer tab on the Ribbon, and then locate the Visual Basic button.

3. In the Visual Basic screen, click the Insert tab on the Ribbon and select the Module button.

4. Type the following code in the empty module.

Sub PrintAllSheetsToPDF()

'SUBROUTINE: PrintAllSheetsToPDF

'DEVELOPER: Ryan Wells

'DESCRIPTION: Combine all your worksheets into one PDF

Dim strSheets() As String

Dim strfile As String

Dim sh As Worksheet

Dim icount As Integer

Dim myfile As Variant

'Save Chart Sheet names to an Array

For Each sh In ActiveWorkbook.Worksheets

If sh.Visible = xlSheetVisible Then

ReDim Preserve strSheets(icount)

strSheets(icount) = sh.Name

icount = icount + 1

End If

Next sh

If icount = 0 Then 'No charts found. Punch error

MsgBox "A PDF cannot be created because no sheets were found.", , "No Sheets Found"

Exit Sub

End If

'Prompt for save location

strfile = "Sheets" & "_" _

& Format(Now(), "yyyymmdd_hhmmss") _

& ".pdf"

strfile = ThisWorkbook.Path & "\" & strfile

myfile = Application.GetSaveAsFilename _

(InitialFileName:=strfile, _

FileFilter:="PDF Files (*.pdf), *.pdf", _

Title:="Select Folder and File Name to Save as PDF")

If myfile <> "False" Then 'save as PDF

ThisWorkbook.Sheets(strSheets).Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _

myfile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _

IgnorePrintAreas:=False, OpenAfterPublish:=True

Else

MsgBox "No File Selected. PDF will not be saved", vbOKOnly, "No File Selected"

End If

End Sub

6. Press the F5 button key to run the code. You'll be prompted to save the PDF file created.