Excel Userform实时倒计时

时间:2018-06-27 17:41:47

标签: excel vba excel-vba

我创建了一个带有文本标签的简单用户窗体。我将这段代码放在激活用户表单上,以从目标时间开始运行剩余时间。一旦运行就很好了,但是它不会倒数...秒不会掉下来...等等。

代码

Private Sub UserForm_activate()
targtime = DateValue("28 Jun 2018") + TimeValue("18:37:00")
remtime = targtime - Now
Me.Label1 = Int(remtime) & " Days " & Format(remtime - Int(remtime), "HH:MM:SS")
End Sub

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

将其置于无限循环中,它将开始每秒滴答滴答

Private Sub UserForm_Activate()

    Dim remTime As Date

    While True
        remTime = DateValue("28 Jun 2018") + TimeValue("18:37:00") - Now
        Me.Label1 = Int(remTime) & " Days " & Format(remTime - Int(remTime), "HH:MM:SS")
        Me.Repaint
        Application.Wait Now + #12:00:01 AM#
    Wend


End Sub