troubleshooting Question

Macro to save file copy on close

Avatar of vartanu
vartanu asked on
Visual Basic ClassicMicrosoft Excel
5 Comments1 Solution218 ViewsLast Modified:
Hi guys,

I'm using the folowing code to create a copy of an excel (in a separate folder) file everytime this gets data changes.
"
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim wb As Workbook, sName As String, sDir As String
     
    sName = Format(Date, "dd-mm-yyyy")
    sDir = "P:\starpro\Plateau 1\11 Table Upload\System Static Data\Functional Design\FS Bak"
    Set wb = ThisWorkbook
    ChDir "P:\starpro\Plateau 1\11 Table Upload\System Static Data\Functional Design\FS Bak\Tables Bak"
    With wb
        .SaveCopyAs .Name & (sName) & ".xls"
    End With

End Sub
"

The problem is that this is creating a file "file1 and the date.xls" but when I go back in to that file and change some data, this actual code will overwrite the copy everytime.
So I want to have a diferent copy everytime a data is change and not to overwrite the file.
Example:
I go once on "File3.xls" i do a change and i get saved as a copy "File3_august6.xls" in ABC folder
I go again to "File3.xls" i do another change and i get saved something like "File3_august6_2.xls" in ABC folder
And keep saving copies everytime data is change.

Thank you very much in advance.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 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 5 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