Chadi N
Chadi N

Reputation: 441

Application.OnTime isn't running automatically

I'm trying to close my workbook after a certain amount of time.

I'm using 10 seconds for now just to test it but it's not working automatically.

I have to run the code once by myself.

here's my code in module.

Public Sub fermeoutil()

Workbooks("OUTIL_CRN.xlsm").Save
Workbooks("OUTIL_CRN.xlsm").Close
Call test
End Sub

Sub test()
Application.OnTime Now + TimeValue("00:00:10"), "fermeoutil"

End Sub


Upvotes: 1

Views: 955

Answers (1)

Tom Brunberg
Tom Brunberg

Reputation: 21033

In ThisWorkbook add procedure Workbook_Open with following code:

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

In Module1 keep your current procedure fermeoutil() removing the call to test:

Public Sub fermeoutil()
    Workbooks("OUTIL_CRN.xlsm").Save
    Workbooks("OUTIL_CRN.xlsm").Close
End Sub

Your call to test (or Workbook_Open() as it is now called), is not needed, as you have left out the last argument of Application.OnTime, namely Schedule which is optional and has a default value of True meaning the event will be recurring. Not sure if that really is your meaning, since you are closing the wb.

Upvotes: 1

Related Questions