How to Change or Export Excel Files into PDF?
Below are the examples of exporting Excel files into PDF.
Example #1
Save Spreadsheet as PDF File!
For example, look at the below data or chart, which shows the department hierarchy.
We can choose “Save As” from the “File” option and select the “Save as type” as “PDF.”
It may still be the file as a “PDF,” but look how it looks when we open it.
It came on two different sheets in PDF, which makes no sense whatsoever. If you click “Save As” and select “PDF” as the file type, this happens when the file or spreadsheet content exceeds the size of a single PDF sheet.
We need to select the content area when trying to save it as a “PDF” file. Then, press “Ctrl + P” to open below the print previewPrint PreviewPrint preview in Excel is a tool used to represent the print output of the current page in the excel to see if any adjustments need to be made in the final production. Print preview only displays the document on the screen, and it does not print.read more window.
Under “Settings,” choose “Fit Sheet on One Page.”
Now, click on “Save As” in the same window and choose “PDF” as the file type.
As a result, it will fit the whole data into a single PDF page.
It is a common technique we all have used regularly.
However, what if a button can produce a PDF file of the Excel spreadsheet? We can do this by using VBA coding. Follow the below steps to create VBA codeSteps To Create VBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more to convert the Excel sheet as PDF.
Example #2 – Using VBA Code
The following are the steps to convert an Excel sheet to PDF using VBA code.
Step 1: We must first Declare variables in VBADeclare Variables In VBAVariable declaration is necessary in VBA to define a variable for a specific data type so that it can hold values; any variable that is not defined in VBA cannot hold values.read more, as shown below.
Step 2: Now, set “Workbook” and “Worksheet” reference to variables “Ws” and “Wb.”
Step 3: To give the file a name, we need to record the current time, i.e., the time of running the code. Then, we must set the time to the variable “SaveTime.”
Step 4: Now, we need to save the PDF file to the exact location of the workbook that we are working on. Assign this value to the variable “Save Path.”
Step 5: To create a unique file name, we need to combine the file names and time..
Step 6: After that, we need to give the option of choosing the destination path to save the file.
Step 7: Now, we need to create a PDF file.
In case any error occurs, we need to handle this as well. So, the below code will do the same.
Below is the full code for your reference.
Code:
Sub Excel_To_PDF()
‘Declare Variables Dim Ws As Worksheet Dim Wb As Workbook Dim SaveTime As String Dim SaveName As String Dim SavePath As String Dim FileName As String Dim FullPath As String Dim SelectFolder As Variant
‘Set Variables On Error GoTo EH Set Wb = ActiveWorkbook Set Ws = ActiveSheet
‘Record Current Time SaveTime = Format(Now(), “yyyy mm dd _ hhmm”)
‘Record Current Workbook Folder Path Address SavePath = Wb.Path If SavePath = "" Then SavePath = Application.DefaultFilePath End If SavePath = SavePath & ""
‘Give File a Name SaveName = “PDF” FileName = SaveName & “_” & SaveTime & “.pdf”
‘Instruct Where to save FullPath = SavePath & FileName
‘Enable folder picker to choose where to save the file SelectFolder = Application.GetSaveAsFilename _ (InitialFileName:=FullPath, _ FileFilter:=“PDF Files (*.pdf), *.pdf”, _ Title:=“Select Folder and FileName to save”)
‘Create PDF File If SelectFolder <> “False” Then Ws.ExportAsFixedFormat _ Type:=xlTypePDF, _ FileName:=SelectFolder, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=False End If
exitHandler: Exit Sub EH: MsgBox “Not Able to create PDF file” Resume exitHandler End Sub
We must copy this code and paste it into the module of the Visual Basic Editor. To open the Visual Basic Editor, we must press “Alt + F11.” We get the following option when we run this code by pressing the “F5” key.
We can see that the Excel file is now converted into PDF.
Things to Remember
- PDF may push the extra content to the next sheet if the data does not fit on a single page.The code may create the entire worksheet content as a PDF file.Once we copy the macro code, we need to save the workbook as “Macro-Enabled Workbook.”
Recommended Articles
This article is a guide to the Export Excel into PDF. Here, we discuss how to change/convert an Excel file into PDF using simple Excel and VBA examples and a downloadable Excel sheet. You can learn more about Excel from the following articles: –
- Extract Data from PDF to ExcelVBA ExamplesCreate Excel Blank Invoice TemplateCOMBIN Function in Excel