Archive file if filename does not exist

BrianJN1

Well-known Member
Joined
Jun 15, 2017
Messages
611
Each week I load CSV data into my Workbook from a USB stick.
I have recently chosen to archive the CSV file with the date that the data is accessed.
The source file, no matter what the date, is always the same name.
I can successfully archive the CSV using this statement:
Code:
ActiveWorkbook.SaveAs Filename:="C:\DIR\CSV\Filename" & Format(Date, "yyyymmdd") & ".csv", FileFormat:=xlCSV, CreateBackup:=False

As I am repeatedly tweaking code and layouts within the Workbook I need to access my USB stick but when my macro finds that an archived file of the daily date exists I receive and error.

I created an IF …THEN to check if the file by that date name exists:
Code:
If "C:\DIR\CSV\Filename" & Format(Date, "yyyymmdd") & ".csv" = "" Then
ActiveWorkbook.SaveAs Filename:="C:\DIR\CSV\Filename" & Format(Date, "yyyymmdd") & ".csv", FileFormat:=xlCSV, CreateBackup:=False
End If
Unfortunately if the filename does not exist the file is not created.

What simple means might allow my IF … THEN to do as I intend?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Code:
Dim saveFileName As String

saveFileName = "C:\DIR\CSV\Filename" & Format(Date, "yyyymmdd") & ".csv"
If Dir$(saveFileName) = "" Then
    ActiveWorkbook.SaveAs saveFileName, FileFormat:=xlCSV, CreateBackup:=False
End If

WBD
 
Upvote 0
Solution
Ah! Many thanks, that worked a treat.
Hm. I think that i just got carried away and forgot to save that change. No matter, I can still access your offering, Again, thank you.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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