auto backup with code

rjmdc

Well-known Member
Joined
Apr 29, 2020
Messages
672
Office Version
  1. 365
Platform
  1. Windows
hi
i have auto backup when closing file
excel saves auto 10 minutes however if the book corrupts all code is lost
how can i program my backup module to run every hour?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Hi,

You can create backup of excel file after every one hour.

In workbook events:

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnTime Now() + TimeValue("01:00:00"), "callTimer", False
End Sub

Private Sub Workbook_Open()
    callTimer
End Sub

In module:

VBA Code:
Sub callTimer()
    Application.OnTime Now() + TimeValue("01:00:00"), "COPYBACKUP"
End Sub
'It will create backup of file in the same folder with filename+currentdatetime.
Sub COPYBACKUP()
       ThisWorkbook.SaveCopyAs ThisWorkbook.Path & "\" & Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 5) & Format(Now(), "DDMMYYHHMM") & ".xlsm"
       callTimer
End Sub

Thanks
Saurabh
 
Upvote 0
how does this work?
this is my backup code thill now
where do i add this?

Sub Auto_Save()
Dim saveDate As Date
Dim saveTime As Variant
Dim formatTime As String
Dim formatDate As String
Dim backupFolder As String
Dim FileExt As String
Dim MsgBoxResult As String
Dim ThisFileName As String


MsgBoxResult = MsgBox("Would you like a backup of this file?", vbInformation + vbYesNo)
If MsgBoxResult = vbYes Then

saveDate = Now
FileExt = ".xlsm"

formatDate = Format(saveDate, "MM-DD-YYYY hh.MM")


Application.DisplayAlerts = False
backupFolder = "M:\all\my name\_0 Document Dates\Backups\Log\"

ThisFileName = "Log " & formatDate & FileExt

ActiveWorkbook.SaveCopyAs FileName:=backupFolder & ThisFileName
Application.DisplayAlerts = True

MsgBox "Backup Successfully In The Path " & backupFolder

End If
End Sub
 
Upvote 0
Change
VBA Code:
Sub callTimer()
    Application.OnTime Now() + TimeValue("01:00:00"), "COPYBACKUP"
End Sub

to
VBA Code:
Sub callTimer()
    Application.OnTime Now() + TimeValue("01:00:00"), "Auto_Save"
End Sub
And your code in a Module
And
In workbook events:
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime Now() + TimeValue("01:00:00"), "callTimer", False
End Sub
Private Sub Workbook_Open()
callTimer
End Sub
Forget about
VBA Code:
Sub COPYBACKUP()
       ThisWorkbook.SaveCopyAs ThisWorkbook.Path & "\" & Left(ThisWorkbook.Name, Len(ThisWorkbook.Name) - 5) & Format(Now(), "DDMMYYHHMM") & ".xlsm"
       callTimer
End Sub
 
Upvote 0
if i understand, remove mine from workbook events and put into a module the add yours to workbook events?
 
Upvote 0
VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime Now() + TimeValue("01:00:00"), "callTimer", False
End Sub
Private Sub Workbook_Open()
callTimer
End Sub
In workbook events

And the rest you code and callTimer in a module


BTW It's not my code but

Saurabhj​

 
Upvote 0

Forum statistics

Threads
1,215,007
Messages
6,122,670
Members
449,091
Latest member
peppernaut

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