Countup Timer

jrevard

New Member
Joined
Mar 26, 2011
Messages
17
Hi,

I have seen some examples of Countup Timers, but when I try to implement them into my program they don't work. I code exclusively in the userform and don't see any examples on how to create a countup timer this way. The examples I found use the spreadsheet, but I want to use a label (or something similar) in my form. When I try to figure out where code should go (Worksheet, modules, etc.) I get lost and nothing wants to work. Any suggestions or tutorials?

Thanks,

Justin
 
Here's an example from userform - Timer on user form in Excel VBA - Stack Overflow

Userform code

Code:
Dim nextTriggerTime As Date

Private Sub UserForm_Initialize()
    Label1 = 0
    ScheduleNextTrigger
End Sub

Private Sub UserForm_Terminate()
    Application.OnTime nextTriggerTime, "modUserformTimer.OnTimer", Schedule:=False
End Sub

Private Sub ScheduleNextTrigger()
    nextTriggerTime = Now + TimeValue("00:00:01")
    Application.OnTime nextTriggerTime, "modUserformTimer.OnTimer"
End Sub

Public Sub OnTimer()
    Label1 = Label1 + 1
    ScheduleNextTrigger
End Sub

Then add a module and rename it to modUserformTimer, and place in the following

Code:
Public Sub OnTimer()
    UserForm1.OnTimer
End Sub

That should work on a userform named UserForm1, with a label named Label1.
 
Upvote 0
Thank you, I will try it when I have time. I knew I needed to learn modules (they're like classes I think) and this seems like a great opportunity. I really appreciate this as I have not found an example anywhere else (other than the examples for a worksheet).
 
Upvote 0
Works for me. Good job finding that post on Stack Overflow, I don't know how you did. A search for any of the words in that title produces daunting results.

I tried updating the code to have a time format "00:00:00" but everything I try gives different errors. Current code (below) just seems to concatenate strings together. Any ideas? Thx


Code:
Dim nextTriggerTime As Date

Private Sub UserForm_Initialize()
    Label1 = 0
    ScheduleNextTrigger
    Label1.Caption = Format(Time, "00:00:00")
End Sub

Private Sub Userform_Terminate()
    Application.OnTime nextTriggerTime, "modUserformTimer.OnTimer", Schedule:=False
End Sub

Private Sub ScheduleNextTrigger()
    nextTriggerTime = Now + TimeValue("00:00:01")
    Application.OnTime nextTriggerTime, "modUserformTimer.OnTimer"
End Sub

Public Sub OnTimer()
    Label1 = Label1 + "00:00:01"
    ScheduleNextTrigger
End Sub
 
Upvote 0

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