Warm tip: This article is reproduced from stackoverflow.com, please click
excel excel-vba vba

VBA how to display real time clock in a userform?

发布于 2020-04-10 10:07:53

So I need to display a real-time clock on my form but I am not sure how. I do know that the code:

TimeValue(Now)

will give me the current time, but I am not sure how to continually display this on my form.

I have an idea which is to put this code inside of a loop as follows:

Dim bool As Boolean
bool = True
Do While bool = True
    Label1.Caption = TimeValue(Now)
Loop

However I am not sure where to put this code. Any suggestions would be greatly appreciated!

Questioner
thaweatherman
Viewed
31
thaweatherman 2012-11-29 19:05

I solved the issue others were having with the given code in the chosen answer. I removed the latesttime line and i changed Label1.caption = Now() to use Time() instead.

Sub DisplayCurrentTime()
    Dim nextSecond As Date

    nextSecond = DateAdd("s", 1, Now())

    Label1.Caption = Time()

    Application.OnTime _
        Procedure:="DisplayCurrentTime", _
        EarliestTime:=nextSecond
End Sub

I then called this in my userform_initialize function. Label1.caption was changed to the appropriate label on my userform.

Thanks for all of the help!!