+ Reply to Thread
Results 1 to 16 of 16

Application OnTime error

Hybrid View

  1. #1
    Registered User
    Join Date
    08-02-2018
    Location
    Australia
    MS-Off Ver
    7
    Posts
    8

    Application OnTime error

    Hi everyone,

    I'm relatively new to VBA and was looking for a way to repeat an action in a spreadsheet. I stumbled across the Application.OnTime procedure and thought I'd give it a go.

    I tried a piece of code I want to run when a workbook is opened and closed. I got the code to run on opening the workbook and at regular 5 minute intervals but when the workbook is closed, it throws up a Run-time error ‘1004’: Method ‘OnTime’ of object’_Application’ failed. The code is posted below.

    Private Sub Workbook_Open()
    Application.OnTime NextUpdate, "TimRun" , , True
    End Sub
    Public Sub TimRun()
    MsgBox "Sheet Updated!", vbOKOnly, "Hello"
    NextUpdate = Now + TimeValue("00:05:00")
    Application.OnTime NextUpdate, "TimRun"
    End Sub
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnTime NextUpdate, "TimRun", , False
    MsgBox "Are you sure you want to close this workbook?", vbYesNo, "Hello"
    End Sub
    Any help would be most welcome.
    Last edited by jeffreybrown; 08-18-2018 at 09:10 AM. Reason: Please use code tags

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Application OnTime error

    Change this part:

    Application.Run("TimRun")
    This for all instances where you have , "TimRun",
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  3. #3
    Registered User
    Join Date
    08-02-2018
    Location
    Australia
    MS-Off Ver
    7
    Posts
    8

    Re: Application OnTime error

    Hi Keebellah,

    Thanks for the reply.

    Just so I've got this right, the code for the workbook open command would look something like this:

    Private Sub Workbook_Open()
    Application.Run("TimRun") , , True
    End Sub
    Correct??
    Last edited by jeffreybrown; 08-18-2018 at 09:10 AM. Reason: Please use code tags

  4. #4
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Application OnTime error

    No I said the section with TimRun mhad to be corrected

    All instances of AppliCation.OnTime !!!!

    Private Sub Workbook_Open()
    Application.OnTime NextUpdate, Application.Run("TimRun") , , True
    End Sub

  5. #5
    Registered User
    Join Date
    08-02-2018
    Location
    Australia
    MS-Off Ver
    7
    Posts
    8

    Re: Application OnTime error

    Whoops!! My bad.

    I'll change all the instances and see how it goes.

    Thanks again.

  6. #6
    Registered User
    Join Date
    08-02-2018
    Location
    Australia
    MS-Off Ver
    7
    Posts
    8

    Re: Application OnTime error

    Hello,

    So I updated the code to change all instances of "TimRun". I now have another problem.

    When the file is opened, the message displays and when the OK button is clicked, the message immediately displays again instead of every 5 minutes as I thought it would. This happens constantly and the message doesn't disappear but just keeps running. It looks like the code is stuck in a loop and I have to kill Excel using Task Manager as I can't close the workbook normally.

    Below is the amended code.

    Private Sub Workbook_Open()
    Application.OnTime NextUpdate, Application.Run("TimRun") , , True
    End Sub
    Public Sub TimRun()
    MsgBox "Sheet Updated!", vbOKOnly, "Hello"
    NextUpdate = Now + TimeValue("00:05:00")
    Application.OnTime NextUpdate, Application.Run("TimRun")
    End Sub
    Help
    Last edited by jeffreybrown; 08-18-2018 at 09:10 AM. Reason: Please use code tags

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,056

    Re: Application OnTime error

    It doesn't look like you declared NextUpdate anywhere? Assuming all your code is in the ThisWorkbook module, your code should be:

    Dim NextUpdate as Date
    Private Sub Workbook_Open()
    TimRun
    End Sub
    
    
    Public Sub TimRun()
    MsgBox "Sheet Updated!", vbOKOnly, "Hello"
    NextUpdate = Now + TimeValue("00:05:00")
    Application.OnTime NextUpdate, "TimRun"
    End Sub
    
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnTime NextUpdate, "TimRun", , False
    MsgBox "Are you sure you want to close this workbook?", vbYesNo, "Hello"
    End Sub
    Everyone who confuses correlation and causation ends up dead.

  8. #8
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Application OnTime error

    Good one Rory, I forget to check if the OP had indeed declared the variable, assumptions are deadly

  9. #9
    Registered User
    Join Date
    08-02-2018
    Location
    Australia
    MS-Off Ver
    7
    Posts
    8

    Re: Application OnTime error

    Hi Rory and Keebellah,

    Thanks for the replies. I will update the code to include the NextUpdate declaration and see how it goes.

    I'm curious to know why the Workbook Open procedure doesn't state the Application.OnTime function as true i.e.

    Application.OnTime NextUpdate, "TimRun" , , True
    yet the Workbook BeforeClose function does

    Application.OnTime NextUpdate, "TimRun" , , False
    Slightly confused
    Last edited by jeffreybrown; 08-18-2018 at 09:11 AM. Reason: Please use code tags

  10. #10
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Application OnTime error

    The default is always True, that's the reason.
    You don't have to turn in 'ON' but you do have to turn it 'OFF'
    Hope this helps you.
    Happy coding

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Application OnTime error

    @YellowCows,

    Please use code tags when posting code!
    HTH
    Regards, Jeff

  12. #12
    Registered User
    Join Date
    08-02-2018
    Location
    Australia
    MS-Off Ver
    7
    Posts
    8

    Re: Application OnTime error

    Hi Keebellah,

    Thanks for the information. I made the changes to the code but now the error which I originally posted has appeared again (Run-time error ‘1004’: Method ‘OnTime’ of object’_Application’ failed). The Workbook Open and TimRun procedures run OK but the error appears when I try and close the workbook. It looks like the Workbook BeforeClose procedure isn't working.

    I have re-posted the code below.

    Private Sub Workbook_Open()
    TimRun
    End Sub
    Dim NexUpdate as Date
    Public Sub TimRun()
    MsgBox "Sheet Updated!", vbOKOnly, "Hello"
    NextUpdate = Now + TimeValue("00:05:00")
    Application.OnTime NextUpdate, "TimRun"
    End Sub
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.OnTime NextUpdate, "TimRun", , False
    MsgBox "Are you sure you want to close this workbook?", vbYesNo, "Hello"
    End Sub
    The Workbook Open and Workbook BeforeClose procedures are located in the This Workbook object whereas the TimRun procedure is located in a separate module. Does this matter?

    jeffreybrown - apologies. I have used the code tags in this post and will do so from now on (still learning )

  13. #13
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Application OnTime error

    You should use the NexUpdate value not the Now() + etc

  14. #14
    Registered User
    Join Date
    08-02-2018
    Location
    Australia
    MS-Off Ver
    7
    Posts
    8

    Re: Application OnTime error

    Hi Keebellah,

    I'm a little confused about the latest reply. The Workbook BeforeClose procedure has the NextUpdate value stated. I want the update to run every 5 minutes which is why I used the Now() + in the TimRun procedure.

    How will the TimRun procedure run every 5 minutes if the time period is not specified?

  15. #15
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Application OnTime error

    TimRun takes care of the NewtUpdate value

    Global NexUpdate as Date
    Public Sub TimRun()
    MsgBox "Sheet Updated!", vbOKOnly, "Hello"
    NextUpdate = Now + TimeValue("00:05:00")
    Application.OnTime NextUpdate, "TimRun"
    End Sub
    You have to see the NextUpdate to GLOBAL not just DIM if it's not set Global it will not be recognized outside the module

    And the before close, I would do it like this:

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    if MsgBox("Are you sure you want to close this workbook?", vbYesNo, "Hello") <> vbyes then
       Cancel = True
       Exit Sub
    Endif
    Application.OnTime NextUpdate, "TimRun", , False
    End Sub

  16. #16
    Registered User
    Join Date
    08-02-2018
    Location
    Australia
    MS-Off Ver
    7
    Posts
    8

    Re: Application OnTime error

    Hi Keebellah,

    Finally got around to trying the update you suggested and .......................................... it worked!!!!

    I hadn't heard of the GLOBAL declaration before but it all makes sense now.

    Thanks for the explanation and all your help.


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Application OnTime Error
    By EssoExplJoe in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-24-2016, 12:19 PM
  2. [SOLVED] Turn off Application.Ontime when Error occurs
    By cmore in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-22-2016, 03:53 AM
  3. VBA - Application.Ontime Cancel Fails to Method 'ONTIME' of Object 'Application'
    By LordAzuRa in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-10-2016, 03:21 PM
  4. [SOLVED] Cancelling Application.ontime Error 1004
    By Kramxel in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-15-2015, 08:24 AM
  5. [SOLVED] 1004 error when using application.ontime
    By gdecker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-19-2013, 12:52 PM
  6. Error with application.ontime...macro runs twice next day..
    By gokzee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-18-2013, 11:41 PM
  7. [SOLVED] VBA macro application.ontime timevalue error
    By Jack7774 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-05-2013, 11:58 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1