SaveCopyAs without overwriting file if it already exists.

ExelTime

New Member
Joined
Dec 21, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I have workbook that has a button to archive the current status of the file in a different location. Right now, am using the SaveCopyAs command to save the new copy. The problem I am running into is that if the archive copy has already been done, it gets overwritten. I would like to stop the macro if the file being saved already exists (will add a MsgBox to inform user of what is happening). Any help would be appreciated.

VBA Code:
Sub Test ()

Dim FilePath As String, ArchWB As String
    FilePath = Environ("userprofile") & "\Reports"
    ArchWB = "Report_" & Format(Date, "YYYY") - 1 & ".xlsm"

    ThisWorkbook.SaveCopyAs Filename:= FilePath & "\" & ArchWB

' If file already exists, I would like to have a message pop-up, alerting user, then cancel the save

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
How about
VBA Code:
Sub Test()

Dim FilePath As String, ArchWB As String
    FilePath = Environ("userprofile") & "\Reports"
    ArchWB = "Report_" & Format(Date, "YYYY") - 1 & ".xlsm"
    
    If Dir(FilePath & "\" & ArchWB) <> "" Then
       MsgBox "?"
    Else
      ThisWorkbook.SaveCopyAs Filename:=FilePath & "\" & ArchWB
    End If
' If file already exists, I would like to have a message pop-up, alerting user, then cancel the save

End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub Test()

Dim FilePath As String, ArchWB As String
    FilePath = Environ("userprofile") & "\Reports"
    ArchWB = "Report_" & Format(Date, "YYYY") - 1 & ".xlsm"
   
    If Dir(FilePath & "\" & ArchWB) <> "" Then
       MsgBox "?"
    Else
      ThisWorkbook.SaveCopyAs Filename:=FilePath & "\" & ArchWB
    End If
' If file already exists, I would like to have a message pop-up, alerting user, then cancel the save

End Sub
Works as needed. Thank you Fluff.

I thought I tried something similar to that and couldn't get it to work. I've tried a few things, so I don't remember, but thank you for your help.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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