troubleshooting Question

How can I prevent overwriting an existing file when saving a file using Excel VBA?

Avatar of newparadigmz
newparadigmzFlag for United States of America asked on
Microsoft Excel
6 Comments1 Solution9978 ViewsLast Modified:
I have a macro that saves a file using the following code. If a file already exists with the same name, Windows asks if I want to replace the original file. Selecting Yes, overwrites the file and program ends normally. Selecting No makes the program go into debug mode.

    On Error Resume Next
        MkDir sNewPath
    On Error GoTo 0
    ActiveWorkbook.Worksheets(Array(sWsSheet1, sWsSheet2)).Copy
    ActiveWorkbook.SaveAs sNewPath & "\" & sNewFile & ".xlsx"
    ActiveWorkbook.Close

3 things;

1. How do I prevent the debug mode when selecting No on overwriting

2. Can I make the default action to just save the new file as a copy?
ie; "Copy of SameNameFile"

3. Can I make it an option to either overwrite or save as a copy?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros