• Welcome to รอบรั้วมหาวิทยาลัยราชภัฏลำปาง.
 

Save Excel as PDF Using VBA (Easy Guide)

เริ่มโดย bracewall1, มิ.ย 21, 2025, 03:51 หลังเที่ยง

หัวข้อก่อนหน้า - หัวข้อถัดไป

bracewall1

Sometimes, you may want to save your Excel file as a PDF. This is helpful when you want to share your work but don't want others to edit it. With a small VBA (Visual Basic for Applications) script, you can do this in just one click! Let's learn how to do it step by step. save excel as pdf vba


Why Save as PDF?
Saving Excel as PDF is useful because:

It keeps the layout the same on any computer.

People without Excel can open it.

It's perfect for reports and invoices.

How to Use VBA to Save Excel as PDF
Here's a simple VBA code you can use:

vba
Copy
Edit
Sub SaveAsPDF()
    Dim ws As Worksheet
    Dim filePath As String

    Set ws = ActiveSheet  ' You can change this to a specific sheet
    filePath = Application.ThisWorkbook.Path & "\" & ws.Name & ".pdf"

    ws.ExportAsFixedFormat Type:=xlTypePDF, Filename:=filePath, Quality:=xlQualityStandard
    MsgBox "Sheet saved as PDF at: " & filePath
End Sub
Step-by-Step Instructions
Press Alt + F11 to open the VBA editor in Excel.

Click Insert > Module.

Copy and paste the code above.

Close the editor.

Press Alt + F8, choose SaveAsPDF, and click Run.

That's it! Your active sheet is now saved as a PDF in the same folder as your Excel file.

Tips
To save all sheets, loop through each one using For Each ws In Worksheets.

Change filePath if you want to choose a different folder.

Add OpenAfterPublish:=True to open the PDF after saving.

Final Words
Using VBA to save Excel as a PDF is very simple and fast. You only need to copy the code once. After that, you can save your sheets as PDFs anytime. This method saves time and helps keep your files safe and professional. Try it today!