If you want the PDF to automatically save in the exact same folder where your current Excel file is stored, replace the desktopPath lines with this: fullPath = ActiveWorkbook.Path & "\" & ws.Name & " Use code with caution. Copied to clipboard ⚠️ Pro-Tips for Best Results Page Setup
' Create a clean file name (Sheet Name + Current Date/Time to avoid overwriting) fileName = ws.Name & & Format(Now, "yyyy-mm-dd_hh-mm-ss" fullPath = desktopPath & fileName ' Handle potential errors On Error GoTo ErrorHandler ' Print to PDF and Save excel vba print to pdf and save
Before diving into the code, let's understand the value: If you want the PDF to automatically save
ws.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=fullPath, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=True, _ IgnorePrintAreas:=False, _ OpenAfterPublish:=True excel vba print to pdf and save
Sub ExportSingleSheetToPDF() Dim ws As Worksheet Dim filePath As String Set ws = ActiveSheet filePath = "C:\PDF Reports\" & ws.Name & ".pdf"
'Find the last customer LastRow = wsData.Cells(wsData.Rows.Count, "A").End(xlUp).Row
Hardcoding filenames ( MyReport.pdf ) means old files get overwritten. Instead, use VBA to create unique names using timestamps.