Macro for Autosaving excel file once a day to a different directory with name + date

eimisQ

New Member
Joined
Sep 27, 2018
Messages
5
Hi, I'm looking for a macro code which autosaves excel file once a day while it's open. I have found a code which would suit me but doesn't always work, maybe somebody could help me ?

Code:
Code:
Option Explicit

Private Sub Workbook_Open()
    AutoSave 'Start Autosaving
 
End Sub


Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim dtTime As Date
    'Cancel next save-call.
    dtTime = GetSetting("MyAppName", "AutoSave", "SaveNext")
    Excel.Application.OnTime dtTime, "ThisWorkbook.AutoSave", Schedule:=False
End Sub


Private Sub AutoSave()
    Const lngFileNotFound_c As Long = 0&
    Dim strFileName As String
    Dim dtTime As Date
    'Create file name.
    strFileName = "\\INTELLINET\vieta\K-serveris\Katilinës dokumentai\NEW DB Test\1" & Format$(Now, _
        "yyyy-mm-dd") & "_" & ActiveWorkbook.Name
    If Right$(LCase$(strFileName), 4) <> ".xlsm" Then
        'Unsaved files won't have an ext yet.
        strFileName = strFileName & ".xlsm"
    End If
    'If it's already been saved today, then don't oversave:
    If LenB(Dir(strFileName)) = lngFileNotFound_c Then
        'Above lines remarked out to allow overwriting.
        ThisWorkbook.SaveCopyAs strFileName
    End If
    'Set Time interval to 5 minutes:
    dtTime = DateAdd("n", 1, Now) 'Get next save-time
    'This will be used later to cancel:
    SaveSetting "MyAppName", "AutoSave", "SaveNext", dtTime
    'Schedule next save:
    Excel.Application.OnTime dtTime, "ThisWorkbook.AutoSave"
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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