Macro fails due to date format

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
558
Office Version
  1. 365
Platform
  1. Windows
Hi All,
I have this small macro that copies some files on the hard drive for a backup. Basically it looks for a folder and file with the date as part of the name. No drama. But today it fell over because with the date format of dd-mmm-yyyy, is all good unless the date, like today is less than the 10th of the month. so i need a date format of d-mmm-yyyy.

an example of the folder name is: C:\Users\jeff\Desktop\Current_Results_10\Output_01-Jun-2023)
an example of the file name is: \Main-Report-01-Jun-2023.csv
so when the name of the folder and file is from the 1st to the 9th of the month the vba rightly returns an error of "file cannot be found"

is there a way to make this a liitle more "general" so that I dont have ot change the date format manually or change the name of the folder and file manually?

Here is the code.

As always, your gracious help is always greatly appreciated.

VBA Code:
Sub CopyMainReportToAllForm()

    'Declare Variables
    Dim FSO
    Dim sFile As String
    Dim sSFolder As String
    Dim sDFolder As String

    'This is the file to copy
    sFile = "\Main-Report-" & Format(Date, "dd-mmm-yyyy") & ".csv"

    'Changing to match the source folder path
    sSFolder = "C:\Users\jeff\Desktop\Current_Results_10\Output_" & Format(Date, "dd-mmm-yyyy")
    
    'Changing to match the destination folder path
    sDFolder = "C:\Users\jeff\Desktop\Current_Results_10\All Form\Main-Report-" & Format(Date, "dd-mmm-yyyy") & ".csv"
    
    'Create Object
    Set FSO = CreateObject("Scripting.FileSystemObject")

    'Copy the file
    FSO.CopyFile (sSFolder & sFile), sDFolder

End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Does the date format d-mm-yyyy not work for you for both the single and double digit days?
 
Upvote 0
Thanks Mark. I have always assumed, all be it incorrectly, that the single "d "in the format would only pick up days 1 to 9., not dats 10 ro 31.

I will do as you suggest and see what happens on the 10th of June.

cheers
 
Upvote 0
The results should be as per the table below

Book1.xlsb
AB
1dd/mm/yyyyd/mm/yyyy
201/06/20231/06/2023
302/06/20232/06/2023
403/06/20233/06/2023
504/06/20234/06/2023
605/06/20235/06/2023
706/06/20236/06/2023
807/06/20237/06/2023
908/06/20238/06/2023
1009/06/20239/06/2023
1110/06/202310/06/2023
1211/06/202311/06/2023
1312/06/202312/06/2023
1413/06/202313/06/2023
1514/06/202314/06/2023
1615/06/202315/06/2023
1716/06/202316/06/2023
1817/06/202317/06/2023
1918/06/202318/06/2023
2019/06/202319/06/2023
2120/06/202320/06/2023
2221/06/202321/06/2023
2322/06/202322/06/2023
2423/06/202323/06/2023
2524/06/202324/06/2023
2625/06/202325/06/2023
2726/06/202326/06/2023
2827/06/202327/06/2023
2928/06/202328/06/2023
3029/06/202329/06/2023
3130/06/202330/06/2023
3201/07/20231/07/2023
3302/07/20232/07/2023
Sheet1
 
Upvote 0
Hi Mark858,

well we are now 3 days into double digit day format and everything is working well.

Thanks for fixing that up. Clearly, my assumption about date format needs a bit of updating :-)
 
Upvote 0
In terms of file names and folder names you really want to consider switching to the format yyyy-mm-dd.

The folders and files names sort as text from left to right and you will have all the days across months and years sorting together eg 01 Jan 2022, 01 Jan 2023, 01 Jan 2023 then 02 Jan 2022, 02 Jan 2023, 02 Jan 2024
(you will have 01 where I have Jan)
This makes it very hard to find things back not to mention that it looks untidy.
 
Upvote 0
Just as a general point about the suggestion by Alex.

The date format yyyy-mm-dd is better all round as it is the only date format that is recognised as the correct date in most regions, as it is the international date format (ISO 8601).

It just saves having most of the conversion issues that you see in various posts, and so is much better if people share files with different countries
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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