Countdown Timer With Multiple Segments

BikerDave

New Member
Joined
Mar 23, 2019
Messages
4
I am trying to develop a countdown timer that will countdown multiple segments from a Worksheet and keep track of the total countdown time. The following code works for the first segment, but when I read the second segment, my time starts counting by two, when I reach the third segment it counts down by three, etc.... I'm stuck trying to figure out how to get the timer, or probably more correctly, the displayed time, to countdown every one second.

Thanks in advance for your help.

Code:
Option Explicit


Public Routine
Public STime As Single
Public Segment As Integer
Public SegmentCount As Integer
Public StimeDisplay As Date
Public StimeStarted As Date
Public StimeContinueAutomation As Date
Public TtimeDisplay As Date
Public timeStarted As Date
Public timeContinueAutomation As Date
Public bAbort As Boolean
Public bContinue As Boolean
Public runWhen As Double


Public waitHours As Integer
Public waitMins As Integer
Public waitSecs As Integer


Public swaitHours As Integer
Public swaitMins As Integer
Public swaitSecs As Integer


Public Sub Timer()


    bAbort = False
    bContinue = False


    timeStarted = Now()
    timeContinueAutomation = Now() + TimeSerial(swaitHours, swaitMins, swaitSecs) 'How much time until the process continues the automation
    
    Application.OnTime earliesttime:=timeContinueAutomation, procedure:="continueAutomation", schedule:=True
    Application.OnTime earliesttime:=Now() + TimeSerial(0, 0, 1), procedure:="updateCountDownLabel", schedule:=True  'This line is triggered an additional time each time through sub


End Sub
Sub updateCountDownLabel()
    
    If Not bAbort And Not bContinue Then
        TtimeDisplay = TtimeDisplay - TimeSerial(0, 0, 1)
        StimeDisplay = StimeDisplay - TimeSerial(0, 0, 1)


        frmrun.TxtBoxSTime = Format(StimeDisplay, "HH:MM:SS")
        frmrun.TxtBoxTTime = Format(TtimeDisplay, "HH:MM:SS")
        
        runWhen = Now() + TimeSerial(0, 0, 1)
        'Debug.Print "scheduling " & runWhen
        Application.OnTime earliesttime:=runWhen, procedure:="updateCountDownLabel", schedule:=True
    Else
        Call stopTimerUpdateCountDownLabel
    End If
    
End Sub
Sub stopTimerUpdateCountDownLabel()


        On Error Resume Next
        'Debug.Print "cancelling " & runWhen
        Application.OnTime earliesttime:=runWhen, procedure:="updateCountDownLabel", schedule:=False
        On Error GoTo 0
        
End Sub
Public Sub continueAutomation()


    If Not bAbort And Not bContinue Then
        Call macroToRun
    End If
    
End Sub


Sub macroToRun()


    bContinue = True


    If Segment < (SegmentCount + 1) Then
    
        Segment = Segment + 1
        
        frmrun.TxtBoxCZone = Worksheets(Routine).Range("c" & Segment)
        frmrun.TxtBoxNZone = Worksheets(Routine).Range("c" & Segment + 1)
        frmrun.TxtBoxCCad = Worksheets(Routine).Range("d" & Segment)
        frmrun.TxtBoxNCad = Worksheets(Routine).Range("d" & Segment + 1)
           
        swaitHours = 0                                                  ' Defines next Segment hours
        swaitMins = 0                                                   ' Defines next Segment minutes
        swaitSecs = Worksheets(Routine).Range("b" & Segment) * 60       ' Defines next Total seconds
          
        StimeDisplay = TimeSerial(swaitHours, swaitMins, swaitSecs)
        frmrun.TxtBoxSTime = Format(StimeDisplay, "HH:MM:SS")


        Call Timer
        
    Else


       MsgBox "done"


    End If


End Sub
 
Last edited by a moderator:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top