A simple countdown timer in userform

Wicked_

Board Regular
Joined
Jun 5, 2018
Messages
81
Hi.

I've googled a lot, tried alot, but cant get it to work, so i'll ask here.

Im looking for a simple way to make a countdown timer in a label.

I want it so when the userform loads, a coundown timer from 5 min shows in a label, counting down, in min and sec.

Does anyone know how to do this?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
In your form

Code:
Public ahora, g5, h5, i5
Const wTime = [COLOR=#0000ff]"00:05:00"    'time for test[/COLOR]


Private Sub UserForm_Activate()
    
    g5 = Time
    h5 = Time
    i5 = 0
    Label1.Caption = Format(TimeValue(wTime), "hh:mm:ss")
    ahora = Now
    Call countdown


End Sub


Sub countdown()
    
    i5 = h5 - g5
    h5 = Time
    Label1.Caption = Format(TimeValue(wTime) - TimeValue(Format(i5, "hh:mm:ss")), "hh:mm:ss")
    
    If TimeValue(Label1.Caption) <= 0 Then
        On Error Resume Next
        Application.OnTime EarliestTime:=ahora, Procedure:="toReturn", Schedule:=False
        On Error GoTo 0
        MsgBox "Time's Up!"
        Exit Sub
    End If
    Application.OnTime EarliestTime:=Now + TimeValue("00:00:01"), Procedure:="toReturn", Schedule:=True
    
End Sub

In a module

Code:
Sub toReturn()
    UserForm1.countdown
End Sub
 
Upvote 0
Hi.

I get some error messages when trying that.

I got alot of other code on the userform, so i tried putting it in the very end, after the last "end sub", but then i get the error message:

Compile error:
Only comments may appear after End Sub, End Function or End property.

Also tried to put it within Private Sub UserForm_Initialize(), but then i get this error:

Compile error:
Invalid attribute in Sub of Function

It's there you want me to put it right?

In your form

Code:
Public ahora, g5, h5, i5
Const wTime = [COLOR=#0000ff]"00:05:00"    'time for test[/COLOR]


Private Sub UserForm_Activate()
    
    g5 = Time
    h5 = Time
    i5 = 0
    Label1.Caption = Format(TimeValue(wTime), "hh:mm:ss")
    ahora = Now
    Call countdown


End Sub


Sub countdown()
    
    i5 = h5 - g5
    h5 = Time
    Label1.Caption = Format(TimeValue(wTime) - TimeValue(Format(i5, "hh:mm:ss")), "hh:mm:ss")
    
    If TimeValue(Label1.Caption) <= 0 Then
        On Error Resume Next
        Application.OnTime EarliestTime:=ahora, Procedure:="toReturn", Schedule:=False
        On Error GoTo 0
        MsgBox "Time's Up!"
        Exit Sub
    End If
    Application.OnTime EarliestTime:=Now + TimeValue("00:00:01"), Procedure:="toReturn", Schedule:=True
    
End Sub

In a module

Code:
Sub toReturn()
    UserForm1.countdown
End Sub
 
Upvote 0
Hi again.

Just a followup question.

In the userform i got the timer, i got a button which opens a new userform on top of the "main" one, if i open the "new" userform, and close it (back to the main one, which is allways open beneath the new one), the timer resets hmm.
Is there a way to make it continue counting down, even if i press the button that opens a new userform ontop of the main one?

also, in the userform i got all this, is a multipage form with about 10-12 pages, is there a quick way to add it to a label on the rest as well? I've tried adding more lines to the code, but i "fear" i have to copy the code, edit all the variables, and paste it for all the labels in all the pages, which feels "clustery".
also tried Me.Label2.Caption = Me.Label1.Caption with both Me. and UserForm1. etc. But then the new label shows nothing.

Thanks again :)
 
Upvote 0
Hi again.

Just a followup question.

In the userform i got the timer, i got a button which opens a new userform on top of the "main" one, if i open the "new" userform, and close it (back to the main one, which is allways open beneath the new one), the timer resets hmm.
Is there a way to make it continue counting down, even if i press the button that opens a new userform ontop of the main one?

Change Activate by Initialize

Code:
Public ahora, g5, h5, i5
Const wTime = "00:05:00"    'time for test


Private Sub [COLOR=#0000ff]UserForm_Initialize[/COLOR]()


    g5 = Time
    h5 = Time
    i5 = 0
    Label1.Caption = Format(TimeValue(wTime), "hh:mm:ss")
    ahora = Now
    Call countdown


End Sub


Sub countdown()
    
    i5 = h5 - g5
    h5 = Time
    Label1.Caption = Format(TimeValue(wTime) - TimeValue(Format(i5, "hh:mm:ss")), "hh:mm:ss")
    
    If TimeValue(Label1.Caption) <= 0 Then
        On Error Resume Next
        Application.OnTime EarliestTime:=ahora, Procedure:="toReturn", Schedule:=False
        On Error GoTo 0
        MsgBox "Time's Up!"
        Exit Sub
    End If
    Application.OnTime EarliestTime:=Now + TimeValue("00:00:01"), Procedure:="toReturn", Schedule:=True
    
End Sub

------------------

also, in the userform i got all this, is a multipage form with about 10-12 pages, is there a quick way to add it to a label on the rest as well?


You can put the label on the form, outside the pages, that way you can navigate through the pages and the label will always be visible.
 
Upvote 0
I'm glad to help you. I appreciate your kind comments.
 
Upvote 0

Forum statistics

Threads
1,215,730
Messages
6,126,529
Members
449,316
Latest member
sravya

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