
Therefore, it may be necessary to include file handling procedures to prevent overwriting existing documents and handling errors.
#Vba print selection pdf
#Vba print selection code
If you would prefer the save location to be included in a cell, change the code to reference the sheet and cell containing the file path. To save the file in the correct location, change this list of code: saveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf" To adapt the code examples to your specific needs, you should adjust certain lines of code. Change this: Set cht = ws.ChartObjects("Chart 1").Chart Rather than naming a specific chart, the macro could run based on the active chart.

Set cht = ws.ChartObjects("Chart 1").ChartĬht.ExportAsFixedFormat Type:=xlTypePDF, _ 'Create and assign variables Dim saveLocation As String Dim ws As Worksheet The VBA code below saves a specified chart as a PDF. Rng.ExportAsFixedFormat Type:=xlTypePDF, _ 'Create and assign variables Dim saveLocation As String Dim ws as Worksheet The macro below saves a specified range as a PDF. Selection.ExportAsFixedFormat Type:=xlTypePDF, _ Sometimes, we only want to save a small part of a worksheet to a PDF. The following code prints only the selected cells. Sub SaveActiveWorkbookAsPDF()ĪctiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _ Use the following macro to save all the visible sheets from a workbook. SaveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf"ĪctiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _ 'Create and assign variables Dim saveLocation As String The following code saves the selected sheets as a single PDF.

This section contains the base code to save Excel as PDF from different objects (workbooks, worksheets, ranges, and charts). From a VBA perspective, it is the ExportAsFilxedFormat method combined with the Type property set to xlTypePDF that creates a PDF. Saving Excel workbooks, sheets, charts, and ranges as PDF

Other fixed formats available (xlTypeXPS).VBA Save to PDF Example using all the options.Selecting specific worksheets before saving as PDF.Saving Excel workbooks, sheets, charts, and ranges as PDF.
