I have been trying to modify this code so that it can receive input from a textbox on the same userform rather than having a separate module to take amount of time.
I have been able to make it set the time but then when you push the command button it immediately stops and says times up.
Any Suggestions on how to make this happen?
Below is the Original Code
I have been able to make it set the time but then when you push the command button it immediately stops and says times up.
Any Suggestions on how to make this happen?
Below is the Original Code
- Add a text box (here called tBx1) to your userform (here UserForm1)
- Set the Show Modal Property of your userform to False (i.e. modeless)
- Add Start and Cancel buttons to the userform
- Add the next bit of code to a general module for your workbook
Code:
Public Const AllowedTime As Double = 1 'Number of minutes to count down
Sub TestUserForm()
MsgBox "click Start when you are ready to begin"
UserForm1.Show
End Sub
Code:
Private Sub CommandButton1_Click()
Dim T, E, M As Double, S As Double
T = Timer
Do
E = CDbl(Time) * 24 * 60 * 60 - T 'elapsed time in secs
M = AllowedTime - 1 - Int(E / 60)
S = 59 - Round((E / 60 - Int(E / 60)) * 60, 0)
With tBx1
.Value = Format(CStr(M), "00") & ":" & Format(CStr(S), "00")
End With
DoEvents
Loop Until (Timer - T) / 60 >= AllowedTime
Unload UserForm1
End Sub
Private Sub CommandButton2_Click()
Unload UserForm1
End Sub
Private Sub UserForm_Initialize()
Dim M As Double, S As Double
M = Int(AllowedTime)
S = (AllowedTime - Int(AllowedTime)) * 60
With tBx1
.Value = Format(CStr(M), "00") & ":" & Format(CStr(S), "00")
End With
End Sub
Last edited: