Hi,
I can't see anything wrong with the code. My speculation is that there is something somewhere that says 'timer' instead of 'timer1'.
Instead of creating multiple timers see the attached file which contains the following code changes in red, that should allow you to use the same timer code for all your UserForms.
In the ThisWorkbook code module (to make sure the timer terminates properly):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call StopTimer
End Sub
In the ActivityTracker UserForm code module (to make sure the timer terminates properly):
Private Sub UserForm_Terminate()
Call StopTimer
End Sub
In ordinary code module module2 (timer code):
Option Explicit
'NOTE: 'Public' means the variable can be accessed by any routine in any module in the file
' 'Dim' or 'Private' means the variable can ONLY be accessed by routines in THIS module
Public myGblUserFormObject As Object
Public sGblStopWatchTextBoxName As String
Public sGblPauseResumeCommandButtonName As String
Dim NextTick As Date, t As Date, xElapsedTime As Date, xPreviousElapsedTime As Date
Dim bTimerIsActive As Boolean
Dim bTimerIsPaused As Boolean
Sub StartStopWatch()
Debug.Print "StartStopWatch() called at " & Now()
Call StopTimer
bTimerIsActive = True
bTimerIsPaused = False
xPreviousElapsedTime = 0
t = Now()
Call StartTimer
End Sub
Sub PauseOrRestartTimer()
Debug.Print "PauseOrRestartTimer() called at " & Now()
If bTimerIsActive = True Then
'Toggle Timer is Paused Flag
bTimerIsPaused = Not bTimerIsPaused
myGblUserFormObject.Controls(sGblPauseResumeCommandButtonName).Caption = "Resume"
If bTimerIsPaused = False Then
t = Time
myGblUserFormObject.Controls(sGblPauseResumeCommandButtonName).Caption = "Pause"
Call StartTimer
End If
End If
End Sub
Sub StartTimer()
Debug.Print "StartTimer() called at " & Now()
xElapsedTime = xPreviousElapsedTime + Now() - t
myGblUserFormObject.Controls(sGblStopWatchTextBoxName) = Format(xElapsedTime, "hh:mm:ss")
If bTimerIsPaused = True Then
xPreviousElapsedTime = xPreviousElapsedTime + Now() - t
Else
NextTick = Time + TimeValue("00:00:01") 'July 23, 2017 - was first line in this routine
Application.OnTime NextTick, "StartTimer"
End If
Debug.Print Now, Format(xElapsedTime, "hh:mm:ss")
End Sub
Sub StopTimer()
Debug.Print "StopTimer() called at " & Now()
bTimerIsActive = False
bTimerIsPaused = False
On Error Resume Next
Application.OnTime EarliestTime:=NextTick, Procedure:="StartTimer", Schedule:=False
xPreviousElapsedTime = 0
t = 0
End Sub
In the Task Sheet (Sheet2) code module:
Private Sub CommandButton5_Click()
ActiveWorkbook.Save
Worksheets("Activity Sheet").Activate ' the worksheet you want to be active / selected
Set myGblUserFormObject = ActivityTracker
sGblStopWatchTextBoxName = "TextBox3"
sGblPauseResumeCommandButtonName = "CommandButton6"
myGblUserFormObject.Show
End Sub
If this does not solve your problem, please upload a sample file that does not work properly.
Lewis
Bookmarks