Autosave on scheduled time help

Sickjive

New Member
Joined
Sep 6, 2014
Messages
2
windows7, excel 2010

I have a workbook that' s always open and i want to make autosave it everyday on a scheduled time.
I wrote the following code:

Code:
Sub Autosave()
Application.OnTime TimeValue("18:10:00"), "Autosave"
ChDir "P:\Test"
ActiveWorkbook.SaveAs Filename:=Format(Date, "ddmmyy") & ".xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
End Sub
I have one problem with it, when i run the macro it will immediately create the file in the ddmmyy format. When the scheduled time arrives to autosave the file i get a conflict that says the file name already exist.
What is wrong in the code?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
The problem is that you are using SaveAs and that happens when you first run the macro. Here's one way I think you can go, but I have not tested it:
Code:
Sub Autosave()
Dim fPath As String, fNam As String
Application.OnTime TimeValue("18:10:00"), "Autosave"
fPath = "P:\Test\"
fNam = Format(Date, "ddmmyy") & ".xlsm"
If Not WorkbookOpen(fNam) Then
    ActiveWorkbook.SaveAs Filename:=fPath & fNam, FileFormat:=xlOpenXMLWorkbookMacroEnabled
Else
    ActiveWorkbook.Save
End If
End Sub
Function WorkbookOpen(WorkBookName As String) As Boolean
    WorkbookOpen = False
    On Error GoTo WorkBookNotOpen
    If Len(Application.Workbooks(WorkBookName).Name) > 0 Then
        WorkbookOpen = True
        Exit Function
    End If
WorkBookNotOpen:
End Function
 
Upvote 0

Forum statistics

Threads
1,222,227
Messages
6,164,722
Members
451,912
Latest member
HMF009

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