0

Strange issue. I want to save a sheet as PDF and put the date also in the filename. It has been working all good for this date format 01.01.2022.

But as soon as I change the date to this US format ex. 01/01/2022 , it gives me a runtime error. In the system settings, I also changed it to US region and format. So it can't be that.

Sub SavePDF()

Dim i As Integer

For i = 1 To 1
Sheets(i).ExportAsFixedFormat Type:=xlTypePDF, Filename:="C:\myfile " & Sheets(i).Range("A1") & ".pdf", _
                                   Quality:=xlQualityStandard, _
                                   IncludeDocProperties:=True, _
                                   IgnorePrintAreas:=False, _
                                   OpenAfterPublish:=False

Next

End Sub

A1 is where the date is at.

purpleblau
  • 589
  • 2
  • 7
  • 18
  • 1
    Is that date in `Range("A1")`? If so, it is not a matter of date format. **It is a matter of using a illegal character: "/"**! Windows does not accept file names containing it... – FaneDuru Feb 16 '22 at 13:10
  • @FaneDuru OMG, you're awesome! That's why! Never thought about that one. Any way to go around this? – purpleblau Feb 16 '22 at 13:13
  • All illegal characters: **. " / \ [ ] : ; | ,**... – FaneDuru Feb 16 '22 at 13:13
  • 1
    You simple use `Replace(Range("A1").value, "/","_")`. The format will stay as it is, but the file will be saved with underscore ("_") separator... `01_01_2022` – FaneDuru Feb 16 '22 at 13:15

0 Answers0