Hello. I have a timer that I created for a specific purpose that is activated by a macro assigned button (shape). The problem that I am having is that if the button is clicked more than once while the timer is running the countdown time accelerates. In other words, the number of seconds that it takes off each cycle increases every time the button is clicked. The button sometimes will get clicked an extra time by mistake and I would like to prevent this from impacting the timer. How can I limit the macro from re-firing once it is already running? The code I am currently using is below. Note, there is a bit of noise in it because I actually have the timer running twice per cycle with different times and the times are assigned by cell references along with a delay between the two countdowns. That is why there are also macros with "2" at the end. The ones with no "2" at the end are specific to the first run and the ones with a "2" at the end are specific to the second run. I assume that some additional code will need to be added to the StartTimer macro to achieve what I want to do, but am not 100% sure, thus why I am sharing everything.
Dim gCount As Date
Sub SetTime()
Range("AB2").Value = Application.InputBox("Enter A Time For Timer 1 - Use Format MM:SS")
Call ResetTimer
End Sub
Sub SetTime2()
Range("AD2").Value = Application.InputBox("Enter a Time for Timer 2 - Use Format MM:SS")
End Sub
Sub StartTimer()
Range("AA1").Value = 1
gCount = Now + TimeValue("00:00:01")
Application.OnTime gCount, "EndMessage"
End Sub
Sub StartTimer2()
Range("AA1").Value = 2
gCount = Now + TimeValue("00:00:01")
Application.OnTime gCount, "EndMessage2"
End Sub
Sub EndMessage()
Dim xRng As Range
Set xRng = Application.activesheet.Range("K1")
If Range("AA1") = 0 Then Exit Sub
If Range("AB2") = "False" Then Exit Sub
If Range("AB2") = "" Then Exit Sub
xRng.Value = xRng.Value - TimeSerial(0, 0, 1)
If xRng.Value <= 0 Then
Call Voice
If Range("AA1") = 2 Then Call ResetTimer
If Range("AA1") = 2 Then Exit Sub
If Range("AD2") = "False" Then Range("K1") = ("Set Time 2")
If Range("AD2") = "" Then Range("K1") = ("Set Time 2")
If Range("AD2") = "False" Then Application.Wait (Now + TimeValue("0:00:04"))
If Range("AD2") = "" Then Application.Wait (Now + TimeValue("0:00:04"))
If Range("AD2") = "False" Then Call ResetTimer
If Range("AD2") = "" Then Call ResetTimer
If Range("AD2") = "False" Then Exit Sub
If Range("AD2") = "" Then Exit Sub
Application.ScreenUpdating = False
Columns("AF").Hidden = False
Application.Wait (Now + TimeValue(Range("AF2").Text))
Application.ScreenUpdating = True
Columns("AF").Hidden = True
Call ResetTimer2
Call StartTimer2
Exit Sub
End If
StartTimer
If Range("AA1") = 2 Then Call StartTimer2
End Sub
Sub EndMessage2()
Dim xRng As Range
Set xRng = Application.activesheet.Range("K1")
If Range("AA1") = 0 Then Exit Sub
If Range("AD2") = "False" Then Exit Sub
If Range("AD2") = "" Then Exit Sub
xRng.Value = xRng.Value - TimeSerial(0, 0, 1)
If xRng.Value <= 0 Then
Call Voice2
Application.Wait (Now + TimeValue("0:00:02"))
ResetTimer
Exit Sub
End If
StartTimer2
End Sub
Sub ResetTimer()
Range("AA1").Value = 0
Range("K1").Select
If Range("AB2") = "False" Then Range("K1") = ("Set Time")
If Range("AB2") = "" Then Range("K1") = ("Set Time")
If Range("AB2") = "False" Then Exit Sub
If Range("AB2") = "" Then Exit Sub
Range("K1").Value = activesheet.Range("AB2")
End Sub
Sub ResetTimer2()
Range("AA1").Value = 2
Range("K1").Select
If Range("AD2") = "False" Then Range("K1") = ("Set Time 2")
If Range("AD2") = "" Then Range("K1") = ("Set Time 2")
If Range("AD2") = "False" Then Exit Sub
If Range("AD2") = "" Then Exit Sub
Range("K1").Value = activesheet.Range("AD2")
End Sub
Bookmarks