VBA如何在用户窗体中显示实时时钟? [英] VBA how to display real time clock in a userform?
问题描述
所以我需要在表单上显示一个实时时钟,但是我不确定如何显示.我确实知道该代码:
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!
推荐答案
我解决了其他人在所选答案中使用给定代码遇到的问题.我删除了最新时间行,并更改了Label1.caption = Now()改为使用Time().
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
然后我在userform_initialize函数中调用了此方法. Label1.caption已更改为我的用户窗体上的相应标签.
I then called this in my userform_initialize function. Label1.caption was changed to the appropriate label on my userform.
感谢所有帮助!
这篇关于VBA如何在用户窗体中显示实时时钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!