• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Simple Countdown Timer Help in VBA

wmcnaughton

New Member
I have a pretty involved project with multiple macros and I am stuck on one portion. I have, as part of one of my userforms, four text boxes side by side... TextHours, TextMinutes, TextSeconds and TextFraction (milliseconds). I use this for both a stopwatch (counting up) and a countdown timer (counting down to 0). My stopwatch works perfectly with Start, Stop and Reset buttons using the timer method. I have a command button that toggles between stopwatch and timer and then a pop-up box for user input to set the countdown timer starting time.

My issue is I am having a heck of a time getting a countdown timer that works with these same boxes. I already have a way that populates the boxes with the start times... for instance if I want a 30 second countdown, I can get the TextSeconds box to populate with "30", and if I want a 30 minute countdown, I can get the TextMinutes box to populate with "30" and the TextSeconds box to populate with "00" and so on. I can't seem to come up with a formula to count down to 0 using all four of these boxes. I have searched and searched, and tried 5-6 different macro ideas and I am at a loss. Any help would be greatly appreciated!

Thanks,

-Bill
 
Last edited:
Hi Logit,

Thanks for reaching out. I can't post the entire workbook but I can post the Sub that would contain the countdown timer. It will expose you to how I approached the stopwatch portion... the variable TBS identifies stopwatch (TBS=1) or countdown timer (TBS = 2). As I mentioned before, I have a pop-up box with a textbox where the user can type in a number and then an option button to choose minutes or seconds. Then that value gets entered into the TextMinutes and TextSeconds textboxes, the same textboxes that are used for the stopwatch.

Code:
Private Sub TimeBoxStart_Click()

Dim StartTime, FinishTime, TotalTime, PauseTime
StopIt = False
ResetIt = False
If TextHours.value = 0 And TextMinutes.value = 0 And TextSeconds.value = 0 And TextFraction = 0 Then
  StartTime = Timer
  PauseTime = 0
  LastTime = 0
Else
  StartTime = 0
  PauseTime = Timer
End If

If TBS = 1 Then

StartIt:
  DoEvents
  If StopIt = True Then
    LastTime = TotalTime
    Exit Sub
  Else
    FinishTime = Timer
    TotalTime = FinishTime - StartTime + LastTime - PauseTime
    TTime = TotalTime * 1000
    HM = TTime Mod 1000
    TTime = TTime \ 1000
    hh = TTime \ 3600
    TTime = TTime Mod 3600
    MM = TTime \ 60
    SS = TTime Mod 60
    TextHours.Text = Format(hh, "0")
    TextMinutes.Text = Format(MM, "00")
    TextSeconds.Text = Format(SS, "00")
    TextFraction.Text = Format(HM, "000")
    If ResetIt = True Then
      TextHours.Text = Format(0, "0")
      TextMinutes.Text = Format(0, "00")
      TextSeconds.Text = Format(0, "00")
      TextFraction.Text = Format(0, "000")
      LastTime = 0
      PauseTime = 0
      End
    End If
    GoTo StartIt
  End If

ElseIf TBS = 2 Then
 
' Countdown timer code here'

End If

End Sub
 
Ok ... I understand about not being able to post the entire workbook. Please re-create just the timer portion as it is presently in your workbook.
Attach that for review.
 
Back
Top