Save to PDF (File Name To Equal Cell Value)

pauleapo

Board Regular
Joined
Nov 23, 2005
Messages
73
Hi,

I have something already in use that saves an excel doc to a specific location with the filename based on a cells value.

I am wanting to do the same but printing/saving in a pdf, I can't figure out how I need to change the bottom line to save as pdf and not as xlsm, any help would be appreciated.

VBA Code:
Dim FName           As String
    Dim FPath           As String
     
    FPath = Sheets("Timber Programme").Range("W3").Text
    FName = Sheets("Timber Programme").Range("X3").Text
    ThisWorkbook.SaveAs Filename:=FPath & "\" & FName
 

Some videos you may like

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,238
Try:
VBA Code:
Sub SavePDF()
    Dim FName As String, FPath As String
    FPath = Sheets("Timber Programme").Range("W3").Value
    FName = Sheets("Timber Programme").Range("X3").Value
    Sheets.Select
    ChDir FPath
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FName _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
End Sub
 

pauleapo

Board Regular
Joined
Nov 23, 2005
Messages
73
Try:
VBA Code:
Sub SavePDF()
    Dim FName As String, FPath As String
    FPath = Sheets("Timber Programme").Range("W3").Value
    FName = Sheets("Timber Programme").Range("X3").Value
    Sheets.Select
    ChDir FPath
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FName _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=False
End Sub

Thank you but this gives a compile error 'Duplicate declaration in current scope' and highlights FName As String
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,492
Office Version
  1. 365
Platform
  1. Windows
Thank you but this gives a compile error 'Duplicate declaration in current scope' and highlights FName As String
That usually means that you have the
VBA Code:
Dim FName As String
line listed twice in your code.
Just remove one.
 

pauleapo

Board Regular
Joined
Nov 23, 2005
Messages
73

ADVERTISEMENT

That usually means that you have the
VBA Code:
Dim FName As String
line listed twice in your code.
Just remove one.

Brilliant, thanks, also had to remove Sheets.Select it now works and an absolute treat

Again thanks to you both
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,238
You are very welcome. :) By removing that line of code, you are saving the active sheet only. In the quote below, I assumed you wanted to save the entire workbook.
saves an excel doc
 

pauleapo

Board Regular
Joined
Nov 23, 2005
Messages
73
You are very welcome. :) By removing that line of code, you are saving the active sheet only. In the quote below, I assumed you wanted to save the entire workbook.

Sorry I didn't make it clear, yes I was only saving the worksheet. 😃
 

Watch MrExcel Video

Forum statistics

Threads
1,127,009
Messages
5,622,145
Members
415,881
Latest member
tasic89

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
Top