Print a Cell Range to PDF with VBA in Excel (5 Easy Methods)

Excel VBA print range to pdf

Suppose you have the following dataset.

Method 1 – Define a Range to Print to PDF and use a Cell Value as the PDF Name

Excel VBA print range to pdf

<a href=declaring a range" width="685" height="637" />

Excel VBA print range to pdf

<a href=declaring a range" width="544" height="413" />

Sub range_to_pdf_1() ActiveSheet.Range("B3:F13").ExportAsFixedFormat Type:=0, _ Filename:="C:\Users\Mima\Downloads\" & ActiveSheet.Range("F3").Value, _ Quality:=0, IncludeDocProperties:=False, IgnorePrintAreas:=False, _ openafterpublish:=True End Sub

<a href=declaring a range" width="635" height="319" />

<a href=declaring a range" width="569" height="407" />

Excel VBA print range to pdf

Method 2 – Use the InputBox Method and VBA to Print Selected Content to PDF

Excel VBA print range to pdf

Steps:

Sub range_to_pdf_2() Dim defined_rng As Range Set defined_rng = Application.InputBox(Prompt:= _ "Choose the Specific Range", Title:="Microsoft Excel", Type:=8) defined_rng.ExportAsFixedFormat Type:=0, _ Filename:="C:\Users\Mima\Downloads\" & "PDF", _ Quality:=0, IncludeDocProperties:=False, IgnorePrintAreas:=False, _ openafterpublish:=True End Sub

InputBox Method

InputBox Method

InputBox Method

Excel VBA print range to pdf

Method 3 – Use a Function to Print a Specific Range to PDF

Excel VBA print range to pdf

Steps:

Function range_to_pdf(defined_rng As Range) As Boolean defined_rng.ExportAsFixedFormat Type:=0, _ Filename:="C:\Users\Mima\Downloads\" & "PDF", _ Quality:=0, IncludeDocProperties:=False, IgnorePrintAreas:=False, _ openafterpublish:=True End Function

creating a function

=range_to_pdf(B3:E13)

Excel VBA print range to pdf

creating a function

creating a function

Excel VBA print range to pdf

Method 4 – Print Selected Range to PDF and Name with the Current Date and Time

Excel VBA print range to pdf

Steps:

Sub range_to_pdf_4() ActiveSheet.Range("B3:F13").ExportAsFixedFormat Type:=0, _ Filename:="C:\Users\Mima\Downloads\" & "PDF" & "_" & Format(Now(), _ "yyyymmdd hhmmss"), Quality:=0, IncludeDocProperties:=False, _ IgnorePrintAreas:=False, openafterpublish:=True End Sub

saving pdf with current date and time

saving pdf with current date and time

saving pdf with current date and time

Method 5 – Print Select Ranges from Multiple Sheets to PDF with Excel VBA

Suppose you have the following datasets on two separate sheets.

Excel VBA print range to pdf

Excel VBA print range to pdf

Steps:

Sub range_to_pdf_5() Dim sht1, sht2 As Worksheet Dim combined_sheets Set sht1 = Worksheets("List1") Set sht2 = Worksheets("List2") combined_sheets = Array(sht1, sht2) For Each sht In combined_sheets sht.Select sht.ExportAsFixedFormat Type:=0, _ Filename:="C:\Users\Mima\Downloads\" & sht.Name, Quality:=0, _ IncludeDocProperties:=False, IgnorePrintAreas:=False, openafterpublish:=True Next sht End Sub

Multiple sheets

Multiple sheets

Multiple sheets

Excel VBA print range to pdf

Download Workbook

Print Range.xlsm

Related Articles

Save Saved Removed 0 Tanjima Hossain

TANJIMA HOSSAIN is a marine engineer who enjoys working with Excel and VBA programming. For her, programming is a handy, time-saving tool for managing data, files, and online tasks. She's skilled in Rhino3D, Maxsurf C++, MS Office, AutoCAD, and Excel & VBA, going beyond the basics. She holds a B.Sc. in Naval Architecture & Marine Engineering from BUET and is now a content developer. In this role, she creates tech-focused content centred around Excel and VBA. Apart from. Read Full Bio

6 Comments Pirooz-Rahimi May 13, 2023 at 5:46 PM

THank you very much for this fine and easy to follow section. It helped me a great deal. God Bless You .

Reply

Shamima Sultana May 14, 2023 at 10:58 AM Dear Pirooz Rahimi, You are most welcome and thanks for your appreciation. Regards
ExcelDemy

Thank you very much.
I have this problem after running code:
Run-time error ‘1004’:
Document not saved. the document may be open, or an error may have been encountered when saving.

Reply

Md. Abu Sina Ibne Albaruni May 24, 2023 at 11:02 AM

VBA Code to Convert Excel Range to PDF

Dear KAZEM Thank you for taking the time to read this article. I see that you are facing a problem while executing the code. It is the Run-time error ‘1004’. There are several possible reasons for this error. In your case, I believe you are having this issue because the file path you specified is incorrect or unavailable. Please note that you need to modify the file path according to the destination where you want to save the PDF file. You have to change the file path portion in the Filename variable. For your convenience, I am attaching a photo indicating the exact portion of the code that you need to re-write. If you have any more questions, please let us know in the comments. Regards
Md. Abu Sina Ibne Albaruni
Team ExcelDemy