Professional Documents
Culture Documents
Contents [show]
Overview
In this article I will be demonstrating some of the various ways to create a PDF file from a range in Microsoft Excel. I
will show you how to store it in any location on your computer. We will be recording a macro to help is get the code
and then I’ll show you how to add cell references and the current date and time and many other features to make the
VBA code involved very flexible. Watch the video below first so that you can get an overview of what we are
accomplishing here. Second download the template so that your experience in working with the code below will be
much easier.
Cell references
Static named range
Dynamic named range
Create a named range with code
Adjust to suit by going to the Page Layout tab and choose Page Setup / Page and set your Orientation and % of
Normal to fill the page.
Sub PrintPDFAllPath()
Dim Dpath As String
Dim FileN As Range, MyRange As Range
'turn off screen updating
Application.ScreenUpdating = False
Set FileN = Sheet1.Range("A1")
'Dpath = "C:\Users\Trevor\Desktop\" & FileN 'change your path do not forget the last backslash
Dpath = "C:\Users\Trevor\Desktop\" & FileN & " " & Format(Now, "yyyy-mm-dd hh-mm") 'change your path do not
forget the last backslash
'set the named range for the PDF
With Sheet1
.Range("C6:K" & Cells(Rows.Count, "C").End(xlUp).Row).Name = "PDFRng"
End With
'set range
Set MyRange = Sheet1.Range("PDFRng")
Sheet1.PageSetup.PrintArea = "PDFRng"
'create the PDF
On Error Resume Next
MyRange.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Dpath, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
'error handler
On Error GoTo 0
'clear the page breaks
ActiveSheet.DisplayPageBreaks = False
Application.ScreenUpdating = False
End Sub
Conclusion
There are an endless ways to set file paths that will save your PDF to various locations on your computer. I hope that
the methods used above will help you and get you started in understanding the basics that are involved. this is a
great feature that has been included since Microsoft Excel 2007 onwards. It is often overlooked, so why not put it to
good use in your awesome applications