Timer VBA editing help

acombest

Board Regular
Joined
May 8, 2017
Messages
136
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


  1. Add a text box (here called tBx1) to your userform (here UserForm1)
  2. Set the Show Modal Property of your userform to False (i.e. modeless)
  3. Add Start and Cancel buttons to the userform
  4. 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:
Your code counts down as expected when I do it.
 
Upvote 0
yes but I would like to do away with the module and just have the whole thing on 1 userform. That is the issue I am having
 
Upvote 0
Not sure I understand your problem. I put everything in the form module and it works.
Code:
Option Explicit
Const AllowedTime As Double = 1 'Number of minutes to count down

Private Sub UserForm_Initialize()
    Dim M As Double, S As Double
    M = Int(AllowedTime)
    S = (AllowedTime - Int(AllowedTime)) * 60
    With Me.TextBox1
        .Value = Format(CStr(M), "00") & ":" & Format(CStr(S), "00")
    End With
End Sub

Private Sub CommandButton1_Click()
    Countdown
End Sub

Private Sub Countdown()
    Dim T, E, M As Double, S As Double
    Dim LFS As String, FS As String

    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)
        FS = Format(CStr(S), "00")
        If FS <> LFS Then
            With Me.TextBox1
                .Value = Format(CStr(M), "00") & ":" & FS
            End With
        End If
        LFS = FS
        DoEvents
    Loop Until (Timer - T) >= 5    ' AllowedTime * 60
    Unload Me
End Sub
 
Upvote 0

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