Macro to save file, but only if it doesn't already exist. Want it to NOT overwrite.

courtmae

New Member
Joined
Jul 7, 2014
Messages
2
Forgive me if this question has already been asked, but I can't find an answer anywhere. Seems to be plenty of info about how to save and overwrite a file that already exists, but I need the opposite.

Just a simple save as, and if there is no file, then I want to save it with xxx, but if the file already exists, I need the macro to skip it and go to the next command. When prompted during the macro, if I hit cancel to not overwrite, it goes into debug mode, which is no good.

Any help is greatly appreciated!!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Code:
Sub SaveIF()
Const sFilePath As String = "H:\Desktop\ExpenseReport.xls" ' --- [path]
If Len(Dir$(sFilePath)) = 0 Then ThisWorkbook.SaveAs Filename:=sFilePath
End Sub
 
Upvote 0
:confused: It doesn't seem to work.
Here is what I have:



Sub SaveIF()
Sheets("Sheet13").Select
thisdate = Range("b1").Value

Workbooks.Add
Range("A1").Select
ActiveCell.FormulaR1C1 = "Account:"
Range("A16").Select
ActiveCell.FormulaR1C1 = "Thank you"

Const sFilePath As String = "" & thisdate & " info.xlsx"
If Len("S:\Operations\Info\(sFilePath)") = 0 Then ThisWorkbook.SaveAs Filename:=sFilePath
End Sub



I get a "Constant expression required" error. Even if I remove the variable date, the file doesn't save at all. Thanks again for any help!
 
Upvote 0
try

Code:
Dim sFilePath As String: sFilePath = "S:\Operations\Info\" & thisdate & " info.xlsx"
If Len(sFilePath) = 0 Then ThisWorkbook.SaveAs Filename:=sFilePath
 
Upvote 0

Forum statistics

Threads
1,216,171
Messages
6,129,284
Members
449,498
Latest member
Lee_ray

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