Search code examples
excelvbaontime

Daily timer to automatically run a macro


I am trying to run a macro each day at 9am. I am trying to use the OnTime application and have tested the macro to launch at a specific time but it does not work automatically. I have to manually run the macro to get the message box to pop-up. Any advice?

Sub FinancialAlert()

    Application.OnTime TimeValue("15:33:00"), "Summary of Covered Companies"


    Dim i As Long
    i = 5

    While Workbooks("Montreal Issuers.xlsm").Sheets("Summary of Covered Companies").Cells(i, 5) <> ""

    With Workbooks("Montreal Issuers.xlsm")

        If .Sheets("Summary of Covered Companies").Cells(i, 5).Value = 1 Then

            MsgBox Workbooks("Montreal Issuers.xlsm").Sheets("Summary of Covered Companies").Cells(i, 3).Value & " is issuing their next financial statement tomorrow (" & _
            Format(Workbooks("Montreal Issuers.xlsm").Sheets("Summary of Covered Companies").Cells(i, 4).Value, "[$-409]mmmm d, yyyy;@") & ")."


        End If

        If .Sheets("Summary of Covered Companies").Cells(i, 5).Value = 0 Then

            MsgBox Workbooks("Montreal Issuers.xlsm").Sheets("Summary of Covered Companies").Cells(i, 3).Value & " is issuing their next financial statement today (" & _
            Format(Workbooks("Montreal Issuers.xlsm").Sheets("Summary of Covered Companies").Cells(i, 4).Value, "[$-409]mmmm d, yyyy;@") & ")."


        End If


    End With

    i = i + 1

    Wend

End Sub

Solution

  • This can be done, however, you need to run the on time in the module ThisWorkbook in the Workbook_Open routine:

    Private Sub Workbook_Open()
        Application.OnTime TimeValue("15:33:00"), "Summary of Covered Companies"
    End Sub
    

    A macro won't run standalone. When the workbook is opened this command will schedule it. If the command was only called in the macro itself, Excel will never know until something activates it.