+ Reply to Thread
Results 1 to 5 of 5

VBA countdown timer

Hybrid View

ineedmoneh VBA countdown timer 10-01-2019, 09:39 AM
davesexcel Re: VBA countdown timer 10-02-2019, 09:48 AM
ineedmoneh Re: VBA countdown timer 10-03-2019, 02:30 AM
torachan Re: VBA countdown timer 10-03-2019, 03:25 AM
bakerman2 Re: VBA countdown timer 10-03-2019, 07:32 AM
  1. #1
    Registered User
    Join Date
    01-31-2019
    Location
    varna
    MS-Off Ver
    2016
    Posts
    16

    VBA countdown timer

    Hello,

    I have made a countdown timer and used some VBA code to trigger it. Now i want to remove all formulas used and express everything with VBA, but i simply dont uderstand how to write it. Have tried:
    Dim startdate As Date, enddate As Date
    Dim days As Long, hours As Long, minutes As Long, seconds As Long
    startdate = Now()
    enddate = "31/12/2019 20:00:00"
    
    days = Int(DateValue(enddate) - DateValue(startdate))
    hours = Hour(TimeValue(enddate) - TimeValue(startdate))
    minutes = Minute(TimeValue(enddate) - TimeValue(startdate))
    seconds = Second(TimeValue(enddate) - TimeValue(startdate))
    Range("D8") = days
    Range("F8") = hours
    Range("H8") = minutes
    Range("J8") = seconds
    but then the timer does not work.

    Original file attached. I want to remove all formulas from it and leave only VBA.

    Any guidance or help appreciated.
    Attached Files Attached Files

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: VBA countdown timer

    At the end of the code you need to call another macro, that calls the countdown macro again, you also need a way to stop the code.

    I just thought of placing the word "Stop" in D1 and the loop will stop.

    
    
    Sub CountDwn()
        Calculate
        If [d1] = "Stop" Then Exit Sub
        Application.OnTime Now + TimeValue("00:00:01"), "CntDownTimer"
    
    End Sub
    Sub CntDownTimer()
        Dim startdate As Date, enddate As Date
        Dim days As Long, hours As Long, minutes As Long, seconds As Long
        startdate = Now()
        enddate = "31/12/2019 20:00:00"
    
        days = Int(DateValue(enddate) - DateValue(startdate))
        hours = Hour(TimeValue(enddate) - TimeValue(startdate))
        minutes = Minute(TimeValue(enddate) - TimeValue(startdate))
        seconds = Second(TimeValue(enddate) - TimeValue(startdate))
    
        Range("D8") = days
        Range("F8") = hours
        Range("H8") = minutes
        Range("J8") = seconds
        CountDwn
    End Sub
    Sub StopCntDwn()
        [d1] = "Stop"
    End Sub

  3. #3
    Registered User
    Join Date
    01-31-2019
    Location
    varna
    MS-Off Ver
    2016
    Posts
    16

    Re: VBA countdown timer

    Hi davesexcel,

    Thank you for this! It works almost perfect

    The issue i am having with it now is that when multiple workbooks are opened and i try to close the countdown file, it reopens again.

    I have simple on open and on save code that starts the countdown macro.

    Option Explicit
    
    Private Sub Workbook_Open()
                          
    Application.Run "Module1.CntDownTimer"
       
    End Sub
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
               
    Application.Run "Module1.CntDownTimer"
                        
    End Sub
    Any idea what causes this issue?

    Thanks.

  4. #4
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,411

    Re: VBA countdown timer

    The attached is another approach, press key to continue, picks up the countdown automatically.
    At present hard coded into your end time ( 01/01/2020 00:00:00 )
    You could alter this by putting your terminal date/time in a cell and referencing the code to this.
    torachan.
    Attached Files Attached Files

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,366

    Re: VBA countdown timer

    Try this way.

    In Standard Module.
    Sub CountDwn()
        Calculate
        Application.OnTime Now + TimeValue("00:00:01"), "CntDownTimer"
    End Sub
    
    Sub CntDownTimer()
        Dim startdate As Date, enddate As Date
        Dim days As Long, hours As Long, minutes As Long, seconds As Long
        startdate = Now()
        enddate = "31/12/2019 20:00:00"
    
        days = Int(DateValue(enddate) - DateValue(startdate))
        hours = Hour(TimeValue(enddate) - TimeValue(startdate))
        minutes = Minute(TimeValue(enddate) - TimeValue(startdate))
        seconds = Second(TimeValue(enddate) - TimeValue(startdate))
    
        Range("D8") = days
        Range("F8") = hours
        Range("H8") = minutes
        Range("J8") = seconds
        CountDwn
    End Sub
    
    Sub StopCntDwn()
        On Error Resume Next
        Application.OnTime Now + TimeValue("00:00:01"), "CntDownTimer", , False
    End Sub
    In ThisWorkbook.
    Private Sub Workbook_Open()
        CountDwn
    End Sub
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        ThisWorkbook.Saved = True
        StopCntDwn
    End Sub
    Last edited by bakerman2; 10-03-2019 at 10:07 AM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] hwnd timer pause VBA countdown timer
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-05-2019, 04:25 AM
  2. Countdown timer Working BUT starting the timer repeatedly SPEEDS UP TIMER!
    By Vinospam in forum Excel Programming / VBA / Macros
    Replies: 67
    Last Post: 03-30-2019, 06:42 PM
  3. Countdown timer
    By Shamz41 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-11-2017, 08:17 AM
  4. Countdown timer
    By Maxicell in forum Excel General
    Replies: 0
    Last Post: 01-11-2017, 07:18 AM
  5. Countdown Timer
    By colt seavers in forum Excel General
    Replies: 3
    Last Post: 01-12-2010, 12:23 PM
  6. HELP for COUNTDOWN TIMER
    By CC in forum Excel General
    Replies: 3
    Last Post: 05-08-2006, 07:55 AM

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