JeffGrant
Well-known Member
- Joined
- Apr 7, 2021
- Messages
- 558
- Office Version
- 365
- Platform
- 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.
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