0

I am trying to create a timer that when reaching zero, it calls from the "Database" script, and then repeats the timer again from 30 seconds. Problem is, every time the timer restarts each tick removes more and more and it won't take long until the script starts repeating every 1 second even though the timer starts at 30.

I have been staring at this for ages now, and I can't figure out what is going on. It clearly states 'cell value = cell value - 1 second'.

What am I missing?

Dim IsOffline As String

Sub Repeater()
    
IsOffline = ThisWorkbook.Worksheets(3).Range("BC2")


    If IsOffline = "Online" Then
        
        RunTimer = Now + TimeValue("00:00:01")
        Application.OnTime RunTimer, "NextTick"

    End If
    
    
    
End Sub


Sub NextTick()
    
    If ThisWorkbook.Worksheets(3).Range("BC5").Value <= TimeValue("00:00:00") And IsOffline = "Online" Then
        
        Call Database
        ThisWorkbook.Worksheets(3).Range("BC5").Value = TimeValue("00:00:30")
        Repeater
    
    End If



    If ThisWorkbook.Worksheets(3).Range("BC5").Value > TimeValue("00:00:00") And IsOffline = "Online" Then
        
        ThisWorkbook.Worksheets(3).Range("BC5").Value = ThisWorkbook.Worksheets(3).Range("BC5").Value - TimeValue("00:00:01")
        Repeater

    End If
    

End Sub
3
  • For the sake of it, have you attempted TimeSerial(0,1,0) as opposed to TimeValue("00:00:01") and specified the argument for EarliestTime? Application.OnTime method (Excel) Commented Apr 21, 2022 at 13:38
  • If you are trying to run a macro every 30 seconds, why don't you just use the API SetTimer function? Such as: stackoverflow.com/a/23949000/3688861 stackoverflow.com/a/20272781/3688861 Commented Apr 21, 2022 at 14:41
  • Cyril - Because I had no idea TimeSerial was a thing :) Will definitely look in to it! Tragamor - Mostly because I want to be able to display the timer in a cell, but maybe that's possible with SetTimer too? will look in to it! Thanks! Commented Apr 22, 2022 at 7:11

1 Answer 1

1

In NextTick, if the first If block executes then the second one will also execute (because in the first block you reset BC5)

That should be an If... Else... End If block, not two separate If blocks.

Dim IsOffline As String

Sub Repeater()
    Dim RunTimer
    IsOffline = ThisWorkbook.Worksheets(3).Range("BC2").Value
    If IsOffline = "Online" Then
        RunTimer = Now + TimeValue("00:00:01")
        Application.OnTime RunTimer, "NextTick"
    End If
End Sub

Sub NextTick()
    If IsOffline = "Online" Then
        With ThisWorkbook.Worksheets(3).Range("BC5")
            If .Value <= TimeValue("00:00:00") Then
                'Database 'Call is deprecated
                .Value = TimeValue("00:00:30")
            Else
                .Value = .Value - TimeValue("00:00:01")
            End If
        End With
        Repeater
    End If
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

This makes so much more sense that what I did! Thanks a tons! I am still learning and this is huge! Only thing I don't really understand here is what the purpose of assigning the "tCell" variable is, as I don't see it being called for?
I'd replaced use of tCell with the With block and forgotten to remove it before posting. Edited to remove...
Makes sense! Thanks so much for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.