Search code examples
excelvbaautomation

To run VBA macros after every 5 days


As you see that all below mentioned Macros calling schedules are linked with one another. However I would like to run the first Sub Button1_Click() macro after every 5 days. Currently the application.Ontime does not increase after 23 hours. Can someone please advise how to i setup the first macro Sub Button1_Click() (Hence rest of the macros are linked with Button1 they will run automatically) to run after every 5 days automatically.

Sub Button1_Click()
    Application.OnTime Now + TimeValue("00:00:10"), "Part1"
End Sub

Sub Part1()
    Call DeleteFiles
    
    Application.OnTime Now + TimeValue("00:00:20"), "Part2"
End Sub

Sub Part2()
    Call CopyFiles_r2
    
    Application.OnTime Now + TimeValue("00:00:55"), "Part3"
End Sub

Sub Part3()
    Call MakeFolders
    
    Application.OnTime Now + TimeValue("00:01:40"), "Part4"
End Sub


Sub Part4()
    Call moveMatchedFilesInAppropriateFolders
    
    Application.OnTime Now + TimeValue("00:01:55"), "Part5"
End Sub

Sub Part5()
    Call OrganizeFilesByFileType
    
    Application.OnTime Now + TimeValue("00:02:35"), "Part1"
End Sub

I will be thankful


Solution

  • Well it sort of IS possible, it's just not the BEST way to do something.

    First, take all 5 steps and call them all from your workbook module.
    Make sure it's set to Workbook_Open Trigger:

    Option Explicit
    Private Sub Workbook_Open()
        
        ' > Give you enough time to Pause/Break when you manually open the file.
        Application.Wait Now() + TimeValue("00:00:30")
        
        ' > Run Series
        Call DeleteFiles                            'Part 1
        Call CopyFiles_r2                           'Part 2
        Call MakeFolders                            'Part 3
        Call moveMatchedFilesInAppropriateFolders   'Part 4
        Call OrganizeFilesByFileType                'Part 5
        
        ' > Close WB
        ThisWorkbook.Save
        ThisWorkbook.Close
        
    End Sub
    

    Next, open your task scheduler:

    enter image description here

    Create a new task:

    enter image description here
    enter image description here
    enter image description here
    enter image description here
    Or whatever...

    Here, enter Dir for Excel program and .xlsm file:

    enter image description here

    Finally Save!

    Limitations:
    This may only work when logged in and computer is awake.
    There is a way for you to task schedule a login... but that's a lot of work, and your company's security policy may dislike that.
    I would suggest you use "run at login" in that case.


    <


    UPDATE:

    The Private Sub Workbook_Open() needs to be places in the ThisWorkbook Module:

    enter image description here

    So now every time I open my workbook, it will say "Hello World"

    enter image description here