Adding a countdown timer to userform in VBA

PC_Meister

Board Regular
Joined
Aug 28, 2013
Messages
72
Hello,

I am wondering if it is possible to add a countdown timer to a userform. Here is the design that I had in mind:

a) Insert a textbox on the userform displaying the countdown
b) Allow the user to interact with controls on userform while the countdown hasn't reached 0
c) Take action if countdown reaches 0 and user did not press the right controls

a) Seems a bit straightforward, possibly through recursion. However I can't seem to think of a way for b) and c), in particular if i adopt my recursive approach to a), the user will not be able to interact with the userform.

Any ideas? Thanks in advance
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I started a countdown timer several years back and never got around to polishing it. For what its worth, UserForm1 has a text box formatted to show time as mm:ss, a command button to start the countdown and a command button to cancel. Here's the code, maybe you can make some use of it.
Code:
Private Sub CommandButton1_Click()
Dim t, E, M As Double, S As Double
'AllowedTime is set in sub 'modTestUserForm' for this user form
t = Timer
Do
'next If-End If protects in case clock was started just before midnight and Timer resets during countdown
    If Timer - t < 0 Then
        Unload UserForm1
        MsgBox "Error encountered - start again"
        Exit Sub
    End If
    E = CDbl(Time) * 24 * 60 * 60 - t 'elapsed time in secs
    M = CDbl(AllowedTime) - 1 - Int(E / 60)
    S = 59 - Round((E / 60 - Int(E / 60)) * 60, 0)
    With tBx1
        .Value = Format(CStr(M), "00") & ":" & Format(CStr(S), "00")
    End With
    DoEvents
Loop Until (Timer - t) / 60 >= CDbl(AllowedTime) Or UserForm1.Visible = False 'i.e. Cancel clicked during countdown
    Unload UserForm1
    Beep
    MsgBox "Your Time Is UP!!"
End Sub

Private Sub CommandButton2_Click()
Unload UserForm1
End Sub

Private Sub UserForm_Initialize()
Dim M As Double, S As Double
M = Int(CDbl(AllowedTime))
S = (CDbl(AllowedTime) - Int(CDbl(AllowedTime))) * 60
 With tBx1
    .Value = Format(CStr(M), "00") & ":" & Format(CStr(S), "00")
End With
End Sub
This module can be used to test the timer.
Code:
Public AllowedTime As Variant
Sub TestUserForm()
AllowedTime = InputBox("Enter the time interval to count down in whole minutes - max is 99")
If AllowedTime = "" Then Exit Sub
MsgBox "click Start when you are ready to begin"
UserForm1.Show
End Sub
EDIT: UserForm1 is modeless so user can do things while it counts down.
 
Upvote 0

Forum statistics

Threads
1,223,925
Messages
6,175,421
Members
452,640
Latest member
steveridge

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