Timer with pause function.

VBABeginer_Chappers

New Member
Joined
Mar 8, 2018
Messages
10
Thank you in advance for any help.
I have been trying to create a play sheet for a game using active X controls. I'm stuck at the moment with my timer. I have 3 command buttons and a text box. The text box displays the timer, the buttons are as follows Start Game, Pause Game & Reset Timer.
My code works for starting the timer, pausing the timer and resetting the timer, however when I press the pause button for the second time to recommence the timer I get a type mismatch error.

Please can someone tell me what is wrong with my code?

Code:
Dim GameNo As String
Dim Start, RunTime, ElapsedTime
Dim PauseStart, PauseRunTime, PauseElapsedTime, PauseEnd


-----------------------------------------------------------------------------


Sub StartTimer()




Sheets("Sheet2").Range("L31").Value = 0
PauseElapsedTime = "0"
PauseRunTime = "0"


Start = Timer
Debug.Print Start


Call Timer_Loop


Sheets("Sheet2").TimerBox.Value = ElapsedTime
Application.StatusBar = False


End Sub

---------------------------------------------------------------------------


Sub PauseTimer()


If Sheets("Sheet2").Range("L32").Value = 1 Then


Sheets("Sheet2").Range("L31").Value = 1
Sheets("Sheet2").Range("L32").Value = 0
Sheets("Sheet2").Range("L33").Value = Sheets("Sheet2").TimerBox.Value


PauseStart = Timer


Else


Sheets("Sheet2").Range("L31").Value = 0
Sheets("Sheet2").Range("L32").Value = 1
PauseEnd = Timer
PauseRunTime = Format(((PauseEnd - PauseStart) + PauseRunTime) / 86400, "hh:mm:ss")
Sheets("Sheet2").Range("L34").Value = PauseRunTime


Call Timer_Loop


End If


End Sub

-------------------------------------------------------------------------


Sub ResetTimer()


Sheets("Sheet2").Range("L31").Value = "2"
Sheets("Sheet2").TimerBox.Value = Format(0, "hh:mm:ss")
Sheets("Sheet2").Range("L32").Value = "1"


End Sub

-----------------------------------------------------------------------------


Sub Timer_Loop()


Do While Sheets("Sheet2").Range("L31").Value = 0


    DoEvents
    RunTime = Timer
    ElapsedTime = Format((RunTime - (Start + PauseRunTime)) / 86400, "hh:mm:ss")
    Sheets("Sheet2").TimerBox.Value = ElapsedTime
    Application.StatusBar = ElapsedTime
    
    Loop


End Sub

Thank you.

Chappers
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
I have been looking at the code and tried to simplify it a lot
Code:
Sub StartTimer()




Sheets("Sheet2").Range("L31").Value = 0


Start = Timer - PauseRunTime  'Saves current time as Start
Debug.Print Start ' inputs The start time to the window


Do While Sheets("Sheet2").Range("L31").Value = 0


    DoEvents
    RunTime = Timer
    ElapsedTime = Format((RunTime - Start) / 86400, "hh:mm:ss")
    Sheets("Sheet2").TimerBox.Value = ElapsedTime
    Application.StatusBar = ElapsedTime
    
    Loop


Sheets("Sheet2").TimerBox.Value = ElapsedTime   ' shows elapsed Time in TimerBox
Application.StatusBar = False


End Sub






Sub PressPauseTimer()


If Sheets("Sheet2").Range("L31").Value = 0 Then


Sheets("Sheet2").Range("L31").Value = 1
PauseRunTime = ElapsedTime + PauseRunTime


Else


Call StartTimer


End If


End Sub


Sub ResetTimer()


Sheets("Sheet2").Range("L31").Value = "2"
Sheets("Sheet2").TimerBox.Value = Format(0, "hh:mm:ss")
'Start = Format(0, "hh:mm:ss")
PauseRunTime = Format(0, "hh:mm:ss")
Start = "0"
'PauseRunTime = "0"
End Sub
but I still get the Type mismatch caution when I try to restart the timer post pausing it.
 
Upvote 0

Forum statistics

Threads
1,216,735
Messages
6,132,425
Members
449,727
Latest member
Aby2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top