Delay Excel save function

2019-09-19 06:10发布

问题:

I have a workbook which requires to be saved at the beginning then auto saving every 5 mins, but i need to delay the initial save so that when the workbook opens it waits 30secs then does the save.

This is the code i have, but it runs it automatically:

Private Sub Workbook_Open()
Time = Now() + TimeValue("00:00:30")
Application.OnTime Time, "WaitUntilReady"

Public Sub WaitUntilReady()

savefolder = "C:\Users\" & Environ$("Username") & "\Desktop\"

mypath = savefolder & Format(Date, "dd-mmm-yy")

If Len(Dir(mypath, vbDirectory)) = 0 Then MkDir mypath

On Error Resume Next

ThisWorkbook.SaveAs mypath & "\" & "Practice Monitoring Template" & " - " & Format(Time, "hh.nn") & ".xlsm"

Application.EnableEvents = True
End Sub

回答1:

Time is reserved word in Excel (it returns current time), use this one instead:

Private Sub Workbook_Open()
    Application.OnTime Now() + TimeValue("00:00:30"), "WaitUntilReady"
End Sub


回答2:

Time is an ingegrated function. If you do not declare it as a variable, the line Time = ... should throw an error. If you have declared it, it should actually run fine (not immediately) - it does in my tests. (Of course you should rather change the variable name).



标签: excel vba timer