Consulting

Results 1 to 9 of 9

Thread: I Need Help Edit Code Auto Save Every 5 Minutes

  1. #1

    I Need Help Edit Code Auto Save Every 5 Minutes

    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

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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.

    [VBA]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
    [/VBA]
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    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:
    [VBA]Option Explicit

    Private Sub Workbook_BeforeClose(Cancel As Boolean)

    Application.OnTime timeNextSave, "SaveAgain", , False

    End Sub

    Private Sub Workbook_Open()

    SaveAgain

    End Sub[/VBA]

    In a Standard Module:
    [VBA]Option Explicit

    Public timeNextSave As Date

    Public Sub SaveAgain()

    ThisWorkbook.Save

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

    Application.OnTime timeNextSave, "SaveAgain", , True

    End Sub[/VBA]

    Hope that helps,

    Mark

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    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.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,647
    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:

    [VBA]
    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
    [/VBA]

  6. #6
    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

  7. #7
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,647
    With the suggestions provided you can easily accomplish that yourself; please take that opportunity !

  8. #8
    Quote Originally Posted by snb
    With the suggestions provided you can easily accomplish that yourself; please take that opportunity !
    Thank you

  9. #9
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •