I am experiencing an issue where I created a VBA timer that counts down from a specified time to zero. This macro runs for a while, as such, when I try to open another workbook nothing happens, it is like the macro blocks the other workbook from opening?
My timer sub
Private Sub Timer15_main(play As Boolean) Dim UserInput As String If play Then UserInput = TextBox1.Value 'this is what the user inputs and how long the timer should run Else timer_15_pause_button = False UserInput = "00:15:00" 'this is what the user inputs and how long the timer should run End If 'validate userinput und ensure hh:mm:ss format Select Case Len(UserInput) - Len(Replace$(UserInput, ":", "")) Case 2 'input format is hh:mm:ss Case 1 'input format is mm:ss UserInput = "00:" & UserInput Case 0 'input format is ss UserInput = "00:00:" & UserInput Case Else MsgBox "invalid input" Exit Sub End Select 'we need to convert the string UserInput into a double and 'convert it into seconds (Timer uses seconds!) Dim SecondsToRun As Long SecondsToRun = CDbl(TimeValue(UserInput)) * 24 * 60 * 60 TextBox4.Value = Format$((SecondsToRun / 24 / 60 / 60) + Time(), "hh:mm:ss") Dim TimerStart As Double TimerStart = Timer 'remember when timer starts Do If SecondsToRun - (Timer - TimerStart) < 10 Then TextBox1.BackColor = RGB(255, 0, 0) End If TextBox1.Value = Format$((SecondsToRun - (Timer - TimerStart)) / 24 / 60 / 60, "hh:mm:ss") 'count backwards from 01:15 format as hh:mm:ss DoEvents If timer_15_pause_button = True Then Exit Sub End If Loop While TimerStart + SecondsToRun > Timer 'run until SecondsToRun are over TextBox1.BackColor = RGB(255, 255, 255) 'TextBox4.Value = "" End Sub
DoEvents?DoEventsin the loop body is not a good solution; look intoApplication.OnTime