Hi VJOSHI,
Try the following code, which is also in the attached file.
There are global variables in the Ordinary Code module that store:
a. If the specific timer is enabled
b. The Start Time for that timer
I changed the storage type to Double for the Timers, because that seems to be the data type that is usually used. My personal coding style is to put as little code in the UserForm module as possible, because it seems to be a lot easier for me to debug.
My UserForm has the following control names that are used by the code:
a. LabelTimer1 - Timer1 display
b. LabelTimer2 - Timer2 display
c. CommandButton1 - Starts Timer2
d. CommandButton2 - Stops Timer2
e. CommandButton3 - Resets Timer2
UserForm1 module code:
Option Explicit
Private Sub UserForm_Activate()
Call StartTimer1
Call ProcessTimers
End Sub
Private Sub UserForm_Terminate()
Call StopAllTimers
End Sub
Private Sub CommandButton1_Click()
Call StartTimer2
End Sub
Private Sub CommandButton2_Click()
Call StopTimer2
End Sub
Private Sub CommandButton3_Click()
Call ResetTimer2
End Sub
Ordinary module code (such as Module1):
Option Explicit
Private bEnabledTimer1 As Boolean
Private xStartTimeTimer1 As Double
Private bEnabledTimer2 As Boolean
Private xStartTimeTimer2 As Double
Sub DisplayUserForm1()
UserForm1.Show False
End Sub
Sub StartTimer1()
bEnabledTimer1 = True
xStartTimeTimer1 = Timer()
End Sub
Sub StartTimer2()
bEnabledTimer2 = True
xStartTimeTimer2 = Timer()
End Sub
Sub StopTimer2()
bEnabledTimer2 = False
End Sub
Sub ResetTimer2()
bEnabledTimer2 = False
UserForm1.LabelTimer2.Caption = ""
End Sub
Sub StopAllTimers()
bEnabledTimer1 = False
Call StopTimer2
End Sub
Sub ProcessTimers()
Dim bAtLeastOneTimerEnabled As Boolean
Dim xElapsedTime As Double
Dim LastEtime As Double
bAtLeastOneTimerEnabled = True
Do While bAtLeastOneTimerEnabled = True
bAtLeastOneTimerEnabled = False
''''''''''''''''''''''''''''''''''''
'Timer1 Processing
''''''''''''''''''''''''''''''''''''
If bEnabledTimer1 = True Then
bAtLeastOneTimerEnabled = True
'Get the elapsed time
'Allow Midnight rollover calculation
xElapsedTime = Timer() - xStartTimeTimer1
If xElapsedTime < 0# Then
xElapsedTime = xElapsedTime + 86400#
End If
xElapsedTime = Int(xElapsedTime * 100) / 100
UserForm1.LabelTimer1.Caption = Format(xElapsedTime / 86400, "hh:mm:ss:") & Format(xElapsedTime * 100 Mod 100, "00")
DoEvents
End If
''''''''''''''''''''''''''''''''''''
'Timer2 Processing
''''''''''''''''''''''''''''''''''''
If bEnabledTimer2 = True Then
bAtLeastOneTimerEnabled = True
'Get the elapsed time
'Allow Midnight rollover calculation
xElapsedTime = Timer() - xStartTimeTimer2
If xElapsedTime < 0# Then
xElapsedTime = xElapsedTime + 86400#
End If
xElapsedTime = Int(xElapsedTime * 100) / 100
UserForm1.LabelTimer2.Caption = Format(xElapsedTime / 86400, "hh:mm:ss:") & Format(xElapsedTime * 100 Mod 100, "00")
DoEvents
End If
Loop
End Sub
I hope this helps.
Lewis
Bookmarks