+ Reply to Thread
Results 1 to 2 of 2

Timer Countdown

Hybrid View

  1. #1
    Registered User
    Join Date
    10-28-2011
    Location
    Staffordshire
    MS-Off Ver
    Excel 2003
    Posts
    21

    Timer Countdown

    Hi Folks, would it be possible for you to help modify my code,

    what im trying to do is create a countdown timer from 5 minutes and when the time is up close the excel application completey, this is the code i have now, its probably not right anyway thanks chris
    Private Sub UserForm1_Initialize()
    Dim dteStart As Date, dteFinish As Date
    Dim dteStopped As Date, dteElapsed As Date
    Dim boolStopPressed As Boolean, boolResetPressed As Boolean
    Start_timer:
    dteStart = Time
    boolStopPressed = False
    boolResetPressed = False
    Timer_Loop:
    DoEvents
    dteFinish = Time
    dteElapsed = dteFinish - dteStart + dteStopped
    If Not boolStopPressed = True Then
    Label2 = dteElapsed
    If boolResetPressed = True Then GoTo Start_timer
    GoTo Timer_Loop
    Else
    Exit Sub
    End If
    End Sub
    Last edited by Leith Ross; 10-29-2011 at 06:57 PM. Reason: Added Code Tags

  2. #2
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: Timer Countdown

    I'm not sure how DoEvents works, but I believe that your macro would have to always be running, which means that no other macros or Events would ever execute as long as it was running.

    Try this:

    Sub StartTimer()
        CheckTimer(1)
    End Sub
    
    Sub EndTimer()
        CheckTimer(2)
    End Sub
    
    Sub ResetTimer()
        CheckTimer(3)
    End Sub
    
    Sub CheckTimer(Optional ByVal iAction as Integer)
        Static dtStart as Date
    
        If iAction = 1 Then
            dtStart = Time
        ElseIf iAction = 2 then
            dtStart = 0
        ElseIf iAction = 3 Then
            dtStart = Time
        End If
        
        If dtStart <> 0 Then
            If Time - dtStart >= TimeSerial(0,5,0) Then
                ThisWorkbook.Close True
            Else
                'check every 10 seconds
                '    allows other macros to run
                Application.Ontime Now + TimeSerial(0,0,10), "'" & ThisWorkBook.Name & "'!CheckTimer"
            End If
        End If
    End Sub
    Foxguy

    Remember to mark your questions [Solved] and rate the answer(s)
    Forum Rules are Here

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1