VBA用户形式计时器

时间:2015-06-29 13:33:26

标签: excel vba excel-vba timer

我有一个使用VBA倒数计时器的程序。计时器工作得很好。问题是我只能输入分钟,没有秒。我希望有人能够轻松修复,所以我可以输入秒数。 感谢

在module1中输入时间

Public Const AllowedTime As Double = 1

,代码看起来像这样

    Private Sub CommandButton1_Click()
Dim T, E, M As Double, S As Double

T = Timer
Do
    E = CDbl(Time) * 24 * 60 * 60 - T
    M = AllowedTime - 1 - Int(E / 60)
    S = 59 - Round((E / 60 - Int(E / 60)) * 60, 0)

    With tBx1
        .Value = Format(CStr(M), "00") & ":" & Format(CStr(S), "00")
    End With
    DoEvents
Loop Until (Timer - T) / 60 >= AllowedTime
End Sub

Private Sub poker_Initialize()
Dim M As Double, S As Double
M = Int(AllowedTime)
S = (AllowedTime - Int(AllowedTime)) * 60
 With tBx1
    .Value = Format(CStr(M), "00") & ":" & Format(CStr(S), "00")
End With
End Sub

这是一个简单的工作示例 https://app.box.com/s/211uo88dk02x6il8hqj19wyiv1rli2sj

1 个答案:

答案 0 :(得分:0)

我建议您使用Date变量进行时间计算,因为它更容易。

如果您希望“暂停”按钮起作用,您还需要一个模块级变量。此代码可让您显示分钟和时间。秒。您的AllowedTime变量应为分钟,小数部分为秒,或将其更改为秒数并更改已注释掉的行。

Dim userClickedPause As Boolean ' Gets set to True by the Pause button

Private Sub CommandButton1_Click()

Dim stopTime As Date

    userClickedPause = False
    ' If AllowedTime is the number of minutes with a decimal part:
    stopTime = DateAdd("s", Int(AllowedTime * 60), Now) ' add seconds to current time

    ' If AllowedTime is the number of seconds:
    'stopTime = DateAdd("s", AllowedTime, Now) ' add seconds to current time
    Do
        With tBx1
            .Value = Format(stopTime - Now, "Nn:Ss")
        End With
        DoEvents
        If userClickedPause = True Then
            Exit Do
        End If
    Loop Until Now >= stopTime

End Sub

Private Sub CommandButton2_Click()
    userClickedPause = True
End Sub