Trong module của bạn có sẵn Sub Stopt(), chỉ cần nhét nó vào 1 chỗ thích hợp:
[highlight=vb]Sub StartTimer()
RunWhen = Now + TimeSerial(0, 0, cRunIntervalSeconds)
Application.OnTime earliesttime:=RunWhen, procedure:=cRunWhat, _
schedule:=True
Stopt
End Sub[/highlight]
Private Sub CommandButton1_Click() If CommandButton1.Caption = "Start Clock" And [B2] < [B3] Then CommandButton1.Caption = "Stop Clock" Start Else CommandButton1.Caption = "Start Clock" End If End Sub
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Sub Start() Dim i As Long For i = 1 To 100 If ([B2].Value - [B3].Value) < TimeValue("00:00:00") Then [B2].Value = [B2].Value + TimeValue("00:00:01") Sleep (960) DoEvents Else Sheet1.CommandButton1.Caption = "Start Clock" End If Next If i = 100 Then Call Start End If End Sub
Sub StopTimer()
On Error Resume Next
Application.OnTime earliesttime:=RunWhen, _
procedure:=cRunWhat, schedule:=False
Range("B2") = TimeSerial(0, 0, 0)
Sheets("Sheet1").Protect
Sub Stopt()
If Range("B2").Value = Range("B3").Value Then
Call StopTimer
MsgBox " da Ket thuc!", vbExclamation, Title:=" TEST"
End If
End Sub