Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Save as PDF with Filename as Cell Value

Tags:

excel

pdf

vba

I am trying to save four sheets into a single PDF. The code below is what I have so far. When I use the ActiveSheet.Name command in the file name it works, however when I change it to a range for a cell that is dynamic it no longer works and errors out. Any help would be appreciated.

Sheets(Array("Dashboard Pg 1", "Dashboard Pg 2", "Dashboard Pg 3", _
    "Dashboard Pg 4")).Select
Sheets("Dashboard Pg 1").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\Allen\Desktop\Projects\" & ActiveSheet.Range("K17").Value & ".pdf" _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False
Sheets("Summary").Select
like image 966
Allen Avatar asked Oct 13 '14 23:10

Allen


People also ask

How do I save Excel as PDF automatically?

Follow these easy steps to learn how to convert Excel to PDF online: Click the Select a file button above or drag and drop a file into the drop zone. Select the file you want to convert from XLS or XLSX to PDF format. After uploading the Excel sheet, Acrobat automatically converts it to the PDF file format.


1 Answers

Try this:

Dim strFilename     As String
Dim rngRange        As Range

'Considering Sheet1 to be where you need to pick file name
Set rngRange = Worksheets("Sheet1").Range("K17")

'Create File name with dateStamp
strFilename = rngRange.Value & Format(Now(), "yyyymmdd hhmmss")

Sheets(Array("Dashboard Pg 1", "Dashboard Pg 2", "Dashboard Pg 3", "Dashboard Pg 4")).Select
Sheets("Dashboard Pg 1").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\Allen\Desktop\Projects\" & strFilename & ".pdf" _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False

Sheets("Summary").Select
like image 128
Jur Pertin Avatar answered Oct 27 '22 01:10

Jur Pertin