Prevent save if file exists

KW1M

New Member
Joined
Jul 21, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Thanks to many here; I have a macro working very well to save as a new workbook, clear data, etc... I've searched and haven't found what should be simple.

What I need to prevent now is when someone accidently hits the "Start New Session" macro button I built a second time -- today, it does a save-as of the current worksheet with today's date.

Even though a pop up is displayed saying "do you really want to overwrite" -- sometimes it happens...

I'd like the macro to detect a file already exists, displays a message like "file exists, exiting" and not allow a new file to be generated. (Reason - this macro clears out a ton of entered data from the previous week, and if they click yes - it's painful.)

Thanks, Martin in NH
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try the following code...

VBA Code:
    If Len(Dir(saveas_filename, vbNormal)) > 0 Then
        MsgBox "File already exists, exiting...", vbExclamation, "File Exists"
        Exit Sub
    End If

...where saveas_filename contains the path and filename.

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,214,889
Messages
6,122,097
Members
449,065
Latest member
albertocarrillom

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