• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

I am stuck to find a VBA code to deleted older files

Ankur Kumar

New Member
Hi all,

I have a vba code which automatically take backup of workbook in different location but i am looking a code which automatically deleted older file of workbook from that folder and rest remain in that folder.

Screen Shot 2018-12-20 at 13.32.42.png

here is my code

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next

Application.DisplayAlerts = False

Dim backupfolder As String
backupfolder = "D:\Auto Backup Sales Detail\Closed\"
ThisWorkbook.SaveCopyAs Filename:=backupfolder & " " & Format(Now, "dd-mmm-yy h-mm-ss") & " " & ActiveWorkbook.Name
ThisWorkbook.Save
Application.DisplayAlerts = True
 
End Sub

Thanks!
Ankur
 
Last edited by a moderator:
Hi,
If you want to keep only the latest file you can loop thru the
BackUp folder and delete them prior to saving the new workbook.
They'r maybe code examples in this forum.
 
Back
Top