adding date and time to save-as file name VBA

jardenp

Active Member
Joined
May 12, 2009
Messages
373
Office Version
  1. 2019
  2. 2016
  3. 2013
  4. 2011
  5. 2010
Platform
  1. Windows
I'm puzzled by the problem I'm having:

I want to save the current sheet as a PDF with the filename "Receipt YYYY-MM-DD HH:MM.pdf" (That is YEAR-MONTH-DAY HOUR:MINUTE)

I have this working:
Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "S:\ ...\Receipt " & Format(Now, "yyyy") & "-" & Format(Now, "mm") & "-" & Format(Now, "dd") & " " & Format(Now, "HH") & ".pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True

but when I try to add the minutes, this doesn't work:
Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "S:\ ...\Receipt " & Format(Now, "yyyy") & "-" & Format(Now, "mm") & "-" & Format(Now, "dd") & " " & Format(Now, "HH") & ":" & Format(Now, "nn") & ".pdf", _
        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True
Ive experimented with things along the line of
Code:
Format(Now, "yyyy-dd-mm hh:nn")
but I always get errors.

Is there a simple way to do this that I'm missing?

Thanks,

JP
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
OK, so I just learned I can't use the colon in a file name. So, my question then is how to get "YEAR-MONTH-DAY HOUR_MIN". Thanks.
 
Upvote 0
OK, so I just learned I can't use the colon in a file name. So, my question then is how to get "YEAR-MONTH-DAY HOUR_MIN". Thanks.

try this:
Code:
Format(Now, "yyyy-mm-dd hh-mm")
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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