A complete backup macro

Vallenato

New Member
Joined
Oct 18, 2017
Messages
13
Hello!
I am looking for a excel macro that automatically does a backup each week (if I am not using the file for more than a week it will automatically do a backup as soon as I start the file).

The backup should be named the file name followed with the backup date and be placed in another folder.

If there is more than 10 old backup files in the folder already the macro should delete the oldest.

(If possible (but I guess not) the macro should also automatically once a month send an automatic e-mail to a certain e-mailadress containing a backup of the file. But I guess maybe this is not possible so exclude this wish from the list if it doesn´t work...)

Does anyone have a vba code for this?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Place the below code on "ThisWorkbook" in your VBA editor & it will ask you if you need to take a back-up upon opening the file every Monday (first day of the week) which you may change as desired

Rich (BB code):
Sub workbook_open()

Dim FileName As String
Dim FileExt As String

FileName = Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".") - 1)
FileExt = Right(ActiveWorkbook.Name, (Len(ActiveWorkbook.Name) + 1 - InStr(ActiveWorkbook.Name, ".")))

If Weekday(Date, vbMonday) = 1 Then  ' vbMonday means your week starts on Monday which is weekday #1 
    If MsgBox("Would you take a back up of the file?", vbInformation + vbYesNo) = vbYes Then
        ActiveWorkbook.SaveCopyAs "C:\ ... your path goes here ... " & FileName & " - " & Format(Date, "dd.mmm.yyyy") & FileExt
    End If
End If

End Sub
 
Upvote 0
Hello!
Thanks for the code. Can you also make it function so if there is more than 10 old backup files in the folder already the macro should delete the oldest? Thanks
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,887
Members
449,057
Latest member
Moo4247

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