Macro to save to PDF with date shown in cell G1 as part of file name

C with no eyes

Board Regular
Joined
Nov 26, 2016
Messages
152
Hi,

I have seen many other threads in relation to this however I cannot seem to get it to work. I'm sorry for repeating this question but I've tried so many variations and have given up.

This code does work:


ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"S:\Month & Year End Documents\Sales Ledger Month End\Daily turnover\Daily turnover.pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True


But I want the file name to be "Daily turnover [date shown in cell g1] and cell g1 is actually a merged cell spanning g1:h2

So far I have this but it highlights yellow and requests debugging:


ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"S:\Month & Year End Documents\Sales Ledger Month End\Daily turnover\Daily turnover " & Range("G1").Value & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True


Do I need to format the date? Or is it because g1 is a merged cell?

Can anyone help me, please? I am using Excel 2010
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
If the default Excel date format contains / or \ characters then those are invalid characters in a file name and you must use a different date format in the code.

This saves the active sheet with the G1 date part of the file name formatted as "yyyy-mm-dd".

Code:
Public Sub Save_Active_Sheet_As_PDF()

    Dim PDFfilename As String
    
    PDFfilename = "S:\Month & Year End Documents\Sales Ledger Month End\Daily turnover\Daily turnover " & Format(Range("G1").Value, "yyyy-mm-dd") & ".pdf"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFfilename, _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,687
Members
452,938
Latest member
babeneker

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