I wrote a macro that allows a user to time the duration of their call through a UserForm, while they continue to enter data about their call on an excel sheet.
Most of the time the macro works perfectly but occasionally (randomly it seems) I get an error when attempting to pause/stop the OnTime procedure.
I receive the following error:
"Run-time error '1004':
Method 'OnTime' of object '_Application' failed"
Followed by an alert that says:
"Can't execute code in break mode"
Can someone please help me prevent this error?
The UserForm (frmTimer) contains the following code:
The following code is in a module:
Most of the time the macro works perfectly but occasionally (randomly it seems) I get an error when attempting to pause/stop the OnTime procedure.
I receive the following error:
"Run-time error '1004':
Method 'OnTime' of object '_Application' failed"
Followed by an alert that says:
"Can't execute code in break mode"
Can someone please help me prevent this error?
The UserForm (frmTimer) contains the following code:
Code:
Private Sub cmdCall_Click()
[COLOR=#008000] 'START TIMER[/COLOR]
If cmdCall.Caption = "START" Then
cmdCall.Caption = "PAUSE"
CallStarting = True
CallDuration = 0
CallStartTime = Now
Call StartCallTimer
[COLOR=#008000] 'PAUSE TIMER[/COLOR]
ElseIf cmdCall.Caption = "PAUSE" Then
cmdCall.Caption = "RESUME"
Call StopCallTimer
[COLOR=#008000] 'RESUME TIMER[/COLOR]
ElseIf cmdCall.Caption = "RESUME" Then
cmdCall.Caption = "PAUSE"
CallStarting = False
PrevCallDuration = CallDuration
CallStartTime = Now
Call StartCallTimer
End If
End Sub
The following code is in a module:
Code:
Option Explicit
Public CallDuration As Date, CallStartTime As Date, PrevCallDuration As Date
Public CallStarting As Boolean
Sub StartCallTimer()
Application.OnTime Now + TimeValue("00:00:01"), "Call_Count"
End Sub
Sub StopCallTimer()
[COLOR=#a9a9a9] '>>>Error occasionally occurs here[/COLOR]
Application.OnTime Now + TimeValue("00:00:01"), "Call_Count", Schedule:=False
End Sub
Sub Call_Count()
Dim CallDisplay As String
If CallStarting = True Then
CallDuration = Now - CallStartTime
CallDisplay = Format(CallDuration, "nn:ss")
frmTimer.lblCallTime.Caption = CallDisplay
Call StartCallTimer
Else
CallDuration = PrevCallDuration + (Now - CallStartTime)
CallDisplay = Format(CallDuration, "nn:ss")
frmTimer.lblCallTime.Caption = CallDisplay
Call StartCallTimer
End If
End Sub