Print a Cell Range to PDF with VBA in Excel (5 Easy Methods)
Suppose you have the following dataset.
Method 1 – Define a Range to Print to PDF and use a Cell Value as the PDF Name
- Decide the cell value to use as the PDF name.
- Go to the Developer tab and select Visual Basic. It will open the Visual Basic window.
declaring a range" width="685" height="637" />
- Go to the Insert Tab and choose the Module Option.
declaring a range" width="544" height="413" />
- Type this code in the Module window:
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
- Set the active sheet range here: ActiveSheet.Range(“”)—(B3:F13 in this example.)
- Enter the full save path here: Filename:=””— (C:\Users\Mima\Downloads\)
- Use the file name cell value here: ActiveSheet.Range(“”)—(F3)
- For this purpose, set: Quality:=0 for standard quality, IncludeDocProperties:=False to ignore document properties, IgnorePrintAreas:=False to use the chosen print areas, and openafterpublish:=True.
declaring a range" width="635" height="319" />
declaring a range" width="569" height="407" />
- Check for the correct data in the PDF.
Method 2 – Use the InputBox Method and VBA to Print Selected Content to PDF
Steps:
- As in Method 1, go to the Developer tab and select Visual Basic. It will open the Visual Basic window.
- Go to the Insert Tab and choose the Module Option.
- A Module will be created.
- Type this code in the Module window:
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
- Enter the full save path and filename here: Filename:=””& “”— (C:\Users\Mima\Downloads\ and PDF)
- For this purpose, set: Quality:=0 for standard quality, IncludeDocProperties:=False to ignore document properties, IgnorePrintAreas:=False to use the chosen print areas, and openafterpublish:=True.
- Press F5.
- Select the specific cells to print and press OK on the Input Box.
- The PDF is saved at the chosen location with the correct name.
- Check for the correct data in the PDF.
Method 3 – Use a Function to Print a Specific Range to PDF
Steps:
- As in Method 1, go to the Developer tab and select Visual Basic. It will open the Visual Basic window.
- Go to the Insert Tab and choose the Module Option.
- A Module will be created.
- Type this code in the Module window:
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
- Enter the full save path and filename here: Filename:=””& “”— (C:\Users\Mima\Downloads\ and PDF)
- For this purpose, set: Quality:=0 for standard quality, IncludeDocProperties:=False to ignore document properties, IgnorePrintAreas:=False to use the defined print areas, and openafterpublish:=True.
- Save the code and return to the main sheet.
- Type the following formula in the first cell of the Print column.
=range_to_pdf(B3:E13)
- The PDF is saved at the chosen location with the correct name.
- Check for the correct data in the PDF.
Method 4 – Print Selected Range to PDF and Name with the Current Date and Time
Steps:
- As in Method 1, go to the Developer tab and select Visual Basic. It will open the Visual Basic window.
- Go to the Insert Tab and choose the Module Option.
- A Module will be created.
- Type this code in the Module window:
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
- Set the active sheet range here: ActiveSheet.Range(“”)—(B3:F13 in this example.)
- Enter the full save path and filename here: Filename:=””& “” & “_” & Format(Now(), _— (C:\Users\Mima\Downloads\ and PDF with the Format and Now functions for the date and time.)
- For this purpose, set: Quality:=0 for standard quality, IncludeDocProperties:=False to ignore document properties, IgnorePrintAreas:=False to use the chosen print areas, and openafterpublish:=True.
- Check for the correct data in the PDF.
Method 5 – Print Select Ranges from Multiple Sheets to PDF with Excel VBA
Suppose you have the following datasets on two separate sheets.
Steps:
- As in Method 1, go to the Developer tab and select Visual Basic. It will open the Visual Basic window.
- Go to the Insert Tab and choose the Module Option.
- A Module will be created.
- Type this code in the Module window:
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
- Define sheet names here: Set sht1 = Worksheets(“”),Set sht2 = Worksheets(“”), and combined_sheets = Array( , )—(List1 and List2 in this example).
- Enter the full save path and filename here: Filename:=””& sht.Name—(C:\Users\Mima\Downloads\ and List1 and List2, respectively.)
- For this purpose, set: Quality:=0 for standard quality, IncludeDocProperties:=False to ignore document properties, IgnorePrintAreas:=False to use the chosen print areas, and openafterpublish:=True.
- Press F5.
- Two PDFs are saved at the chosen location, each with the correct name.
- Check for the correct data in the PDFs.
Download Workbook
Print Range.xlsm
Related Articles
- Print to PDF Using Macro Button in Excel
- How to Print to PDF in Excel VBA: With Examples and Illustrations
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
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