Can I Save a Selected Worksheet Range as a PDF with Automated File Name?

rver7014

New Member
Joined
Dec 14, 2016
Messages
1
Hi All,

I'm looking to assign a macro to a command button that allows me to save the selected range to pdf in a predetermined folder in the directory. On top of this I would like to be able to automate to file name based on cell contents in the worksheet.

Is anyone able to provide the code for this macro?

Thanks in advance.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi All,

I'm looking to assign a macro to a command button that allows me to save the selected range to pdf in a predetermined folder in the directory. On top of this I would like to be able to automate to file name based on cell contents in the worksheet.

Is anyone able to provide the code for this macro?

Thanks in advance.
Yes, but you're a bit slim on necessary details. The code below (untested) assumes you want to save to the same folder the source worbook is in, and that the file name you want to assign is in cell A1. Change either to suit.
Code:
Sub PDFofRange()
Dim R As Range
If TypeName(Selection) = "Range" Then
    Set R = Selection
    R.ExportAsFixedFormat xlTypePDF, Filename:=ThisWorkbook.Path & _
        Application.PathSeparator & Range("A1").Value
End If
End Sub
EDIT: Place a forms control command button on your sheet and assign the macro to it.
 
Last edited:
Upvote 0
Welcome to the forum!

In a Module:
Code:
Sub Main()
  With Worksheets("Sheet1")
    Selection.ExportAsFixedFormat xlTypePDF, Environ("temp") & "\" & _
      .Range("A1").Value
  End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,634
Messages
6,125,934
Members
449,275
Latest member
jacob_mcbride

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top