VBA how to display real time clock in a userform?

后端 未结 3 663
青春惊慌失措
青春惊慌失措 2021-01-14 12:03

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 t

3条回答
  •  感动是毒
    2021-01-14 12:34

    Excel has the OnTime method that allows you to schedule the execution of any procedure.

    Just use it to schedule a one-second rhythm.

    Sub DisplayCurrentTime()
      Dim nextSecond As Date
    
      nextSecond = DateAdd("s", 1, Now())
    
      Label1.Caption = Now()
    
      Application.OnTime _
        Procedure:="DisplayCurrentTime", _
        EarliestTime:=nextSecond, _
        LatestTime:=nextSecond
    End Sub
    

    Start the loop by calling DisplayCurrentTime() once, for example in the initialize method of your form.

提交回复
热议问题