PDA

View Full Version : I Need Help Edit Code Auto Save Every 5 Minutes



etheer
06-10-2013, 01:15 PM
Hi
I Have Code Auto Save Every 5 Minutes But Dont Work Code


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

SamT
06-10-2013, 03:55 PM
You need to more subs.

The Workbook_Open sub starts it all by calling WhentoSave

WhenToSave waits 5 minutes then calls SaveThis.

SaveThis saves the book then calls WhenToSave, starting the loop again.

Option Explicit

Private Sub Workbook_Open()
WhenToSave
End Sub

Private Sub WhenToSave()
Application.OnTime Now + TimeValue("00:05:00"), "SaveThis"
End Sub

Private Sub SaveThis()
Me.Save
WhenToSave
End Sub

GTO
06-10-2013, 06:38 PM
Greetings,

A little different take, as I find it easier/simpler to call the sub in a Standard Module. Also, I believe you will want to handle cancelling the OnTime when the workbook gets closed (lest Excel will still want to run it).

In the ThisWorkbook Module:
Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Application.OnTime timeNextSave, "SaveAgain", , False

End Sub

Private Sub Workbook_Open()

SaveAgain

End Sub

In a Standard Module:
Option Explicit

Public timeNextSave As Date

Public Sub SaveAgain()

ThisWorkbook.Save

timeNextSave = Now + TimeSerial(0, 5, 0)

Application.OnTime timeNextSave, "SaveAgain", , True

End Sub

Hope that helps,

Mark

GTO
06-10-2013, 06:42 PM
:doh: ACK.. Change the declaration for timeNextSave to Private. I was trying something different for a moment, and forgot to scope the variable back down...

Some days I wonder what life would be like...if only my brain worked better.

snb
06-11-2013, 01:06 AM
Since you want to save the workbook, this is an intervention on the workbook level.
That's why I would prefer to put all the code into the workbook macromodule:


Private Sub Workbook_Open()
ThisWorkbook.CustomDocumentProperties.Add "shed1", False, 3, Now
schedule
End Sub

Sub schedule()
ThisWorkbook.CustomDocumentProperties("shed1").Value = DateAdd("n", 5, Now)
Application.OnTime ThisWorkbook.CustomDocumentProperties("shed1").Value, "schedule"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnTime ThisWorkbook.CustomDocumentProperties("shed1").Value, "schedule", , False
End Sub

etheer
06-12-2013, 07:34 AM
Thank you GTO and snb for you help me
you can help me agian
I Need edit this code
Sub refresh()
'
' Macro2 Macro
'
' Keyboard Shortcut: Ctrl+k
'
ActiveWorkbook.RefreshAll
End Sub

i need code refresh and auto save every 5 minutes


i hope help me

snb
06-12-2013, 07:42 AM
With the suggestions provided you can easily accomplish that yourself; please take that opportunity !

etheer
06-13-2013, 02:16 AM
With the suggestions provided you can easily accomplish that yourself; please take that opportunity !

Thank you

SamT
06-13-2013, 06:29 AM
Add the Refresh code to the SaveThis sub above. BTW, all my code above goes in the ThisWorkbook Code Page.

About the "Me" Keyword: Me always refers to the Object of the Class Module it is in.

In ThisWorkbook: Me = Only ThisWorkbook, not always the Active workbook.
In UserForm Code Module: Me = ThisForm, not any other form.
In custom Class Module: Me = ThisCustomObject