Option Explicit
Dim TimerEnable As Boolean
Dim TimerStop As Boolean
Dim iValue As Integer
' 啟動
Private Sub CommandButton5_Click()
TimerEnable = True
iValue = 0
WaitFor 1 '每秒計數一次
End Sub
' 緊急
Private Sub CommandButton1_Click()
TimerEnable = False
End Sub
' 停止
Private Sub CommandButton2_Click()
TimerStop = True
End Sub
Public Sub WaitFor(Sec As Long)
Dim t0 As Long
Dim DnCnt As Boolean
Do
t0 = Timer
While Timer - t0 < Sec
DoEvents
Wend
If Not DnCnt Then
iValue = iValue + 1
If iValue >= 10 Then
DnCnt = True
End If
Sheets(1).Range("a1").Value = iValue
Else
iValue = iValue - 1
If iValue <= 1 Then
DnCnt = False
If TimerStop Then
TimerStop = False
TimerEnable = False
Exit Do
End If
End If
Sheets(1).Range("a1").Value = iValue
End If
If Not TimerEnable Then Exit Do
Loop
Sheets(1).Range("a1").Value = iValue
End Sub作者: GBKEE 時間: 2013-5-22 17:58