VBA如何在用户窗体中显示实时时钟?

标签 vba excel

所以我需要在我的表单上显示实时时钟,但我不知道如何。我确实知道代码:

TimeValue(Now)

会给我当前时间,但我不知道如何在我的表单上持续显示它。

我有一个想法,就是将此代码放入循环内,如下所示:

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

但是我不确定该代码放在哪里。任何建议将不胜感激!

最佳答案

Excel 有 the OnTime method它允许您安排任何过程的执行。

只需用它来安排一秒的节奏即可。

Sub DisplayCurrentTime()
  Dim nextSecond As Date

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

  Label1.Caption = Now()

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

通过调用 DisplayCurrentTime() 一次来启动循环,例如在表单的初始化方法中。

关于VBA如何在用户窗体中显示实时时钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13449743/

相关文章:

vba - Excel没有响应VBA

vba - 寻找在 VBA 中使用的函数式最小化器

用于过滤数据并创建新工作表并向其传输数据的 VBA 代码

vba - 嵌套循环在 VBA 中未正确循环 - 一个有效,另一个有效

excel - 减少字符串并保持零完整

excel - 突出显示 Sheet1 中与 Sheet2 中的值匹配的行

sql-server - "Unexpected error from external database driver (1)."

sql - 使用 VBA 遍历记录集

excel - 如何通过 Python win32 将计算成员添加到数据透视表

excel - 如何在 sub 内部使用 Count If 函数?