Print to PDF and name file based on cell value.

snuggles57

New Member
Joined
Oct 9, 2017
Messages
14
I am trying to modify this code so it uses the value From Sheet("Minute") Cell Ref ("B2") as the file name. Any help appreciated.

Sub Printselection()

Dim rng As Range
Dim wks As Worksheet
Dim arr() As String
Dim i As Long: i = 0
For Each rng In Sheets("Minute").Range("E8:E11")
If Trim(rng.Value) <> "" Then
On Error Resume Next
Set wks = Nothing
Set wks = Sheets(rng.Value)
On Error GoTo 0
If wks Is Nothing Then
MsgBox "Sheet " & rng.Value & " does not exist"
Else
ReDim Preserve arr(i)
arr(i) = wks.Name
i = i + 1
End If
End If
Next rng
Dim printSheets As Variant
printSheets = arr
Worksheets(printSheets).PrintOut Preview:=False, ActivePrinter:="Adobe PDF", PrintToFile:=True, PrToFileName:=PSFileName
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I have not used PrintOut to send it to a PDF file. I would do this:
VBA Code:
   Dim FileName As String
   FileName = Worksheets("Minute").Range("B2") & ".pdf"

   Worksheets(printSheets).ExportAsFixedFormat _
      Type:=xlTypePDF, _
      FileName:=FileName, _
      Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, _
      OpenAfterPublish:=False

However, you might need to do this:
VBA Code:
   Dim FileName As String
   FileName = Worksheets("Minute").Range("B2") & ".pdf"

   Worksheets(printSheets).Select
   ActiveSheet.ExportAsFixedFormat _
      Type:=xlTypePDF, _
      FileName:=FileName, _
      Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, _
      OpenAfterPublish:=False
 
Upvote 0
I have a drop list in Cell E8:E11 that allows me to print different sheets based on what sheets are listed in the drop down lists would your code this still allow me to select the Sheets that I want to print. I need different sheets for different reports / emails.

For Each rng In Sheets("Minute").Range("E8:E11")
 
Upvote 0
I modified to this. but it still is using the file name of the spreadsheet when I print to PDF. I need to keep this in the macro

Sub Printselection()

Dim rng As Range
Dim wks As Worksheet
Dim arr() As String
Dim FileName As String
FileName = Worksheets("Minute").Range("B2") & ".pdf"
Dim i As Long: i = 0
For Each rng In Sheets("Minute").Range("E8:E11")
If Trim(rng.Value) <> "" Then
On Error Resume Next
Set wks = Nothing
Set wks = Sheets(rng.Value)
On Error GoTo 0
If wks Is Nothing Then
MsgBox "Sheet " & rng.Value & " does not exist"
Else
ReDim Preserve arr(i)
arr(i) = wks.Name
i = i + 1
End If
End If
Next rng
Dim printSheets As Variant
printSheets = arr
Worksheets(printSheets).PrintOut Preview:=False, ActivePrinter:="Adobe PDF", PrintToFile:=True, PrToFileName:=FileName 'PSFileName


End Sub
 
Upvote 0
Your latest code doesn't use the code I provided so I don't what else I can do help.
 
Upvote 0
Thank you for your help. I wasn't ignoring your previous suggestion just was not sure what I need to replace. I have amended the code as per your suggestion (I think) and it is now Printing with the required file name, but is saving into C:User\My documents. Previously it was saving in the same location as the spreadsheet file. I have a tried to add some code to refer to a path (MyPath) located in Worksheets("Lists").Range("M16") but I am not sure I have the code correct or even if the code is in the correct order. The path is located in the list worksheet as this would be a Template used by multiple people. Appreciate any assistance.

Sub Printselection()

Dim rng As Range
Dim wks As Worksheet
Dim arr() As String
Dim i As Long: i = 0
For Each rng In Sheets("Minute").Range("E8:E11")
If Trim(rng.Value) <> "" Then
On Error Resume Next
Set wks = Nothing
Set wks = Sheets(rng.Value)
On Error GoTo 0
If wks Is Nothing Then
MsgBox "Sheet " & rng.Value & " does not exist"
Else
ReDim Preserve arr(i)
arr(i) = wks.Name
i = i + 1
End If
End If
Next rng
Dim printSheets As Variant
Dim MyPath As String
MyPath = Worksheets("Lists").Range("M16")
Dim FileName As String
FileName = Worksheets("Minute").Range("B2") & ".pdf"
printSheets = arr
Worksheets(printSheets).Select
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=FileName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

End Sub
 
Upvote 0
Change this line
VBA Code:
FileName = Worksheets("Minute").Range("B2") & ".pdf"

to this
Rich (BB code):
FileName = ThisWorkbook.Path & "\" & Worksheets("Minute").Range("B2") & ".pdf"

You do not need MyPath.
 
Upvote 0

Forum statistics

Threads
1,214,540
Messages
6,120,106
Members
448,945
Latest member
Vmanchoppy

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