+ Reply to Thread
Results 1 to 3 of 3

Countdown Timer on a Userform1

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-07-2012
    Location
    Bangalore
    MS-Off Ver
    Office 365
    Posts
    368

    Countdown Timer on a Userform1

    Thank You So Much!
    Attached Files Attached Files
    Last edited by Rajeshkumar R; 12-29-2016 at 11:28 PM. Reason: Query Solved

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Countdown Timer on a Userform1

    Hi Rajeshkumar R,

    Try the attached mofied copy of your file which contains the following code using an API Timer (Application Programming Interfaces). I had problems using your Label (it did not display properly), so I had to replace it. Please note that the timer MUST be stopped (done by the code below) when the UserForm closes and before the file closes or Excel may crash.

    In the ThisWorkbook code module:
    Option Explicit
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
      Call StopUserFormTimer
    End Sub
    In the UserForm1 Code module:
    Option Explicit
    
    Private Sub UserForm_Initialize()
    
      'Initialize the CountDown Timer Label
      With LabelCountDownTimer
        .Font = "Arial"
        .Font.Size = 42
        .Font.Bold = True
        .Caption = ""
        
        .BackColor = RGB(51, 102, 255) 'RGB(51, 102, 255) = ColorIndex 41 = Blue
        .BorderColor = RGB(51, 102, 255)
      End With
      
    End Sub
    
    Private Sub UserForm_Terminate()
      'This stops the 'UserForm Timer' when the UserForm is closing
      Call StopUserFormTimer
    End Sub
    
    Private Sub CommandButton1_Click()
      Call CloseUserForm1
    End Sub
    In Ordinary Code Module ModApiTimer:
    Option Explicit
    'Reference: http://www.mrexcel.com/forum/excel-questions/392397-problems-running-clock-worksheet-cell.html
    'Thank you  Jaafar Tribak
    
    'NOTE: This has not been tested in 64 bit Excel
    
    #If VBA7 And Win64 Then
      'All of the Win64 lines are supposed to be RED in 32 bit Excel
    
      ' Returns an unsigned long pointer (nIDEvent) if the timer is created. Failure returns a zero.
      Private Declare PtrSafe Function SetTimer _
          Lib "User32.dll" _
              (ByVal hWnd As LongPtr, _
               ByVal nIDEvent As LongPtr, _
               ByVal uElapse As Long, _
               ByVal lpTimerFunc As LongPtr) _
          As LongPtr
    
      Private Declare PtrSafe Function KillTimer _
          Lib "User32.dll" _
              (ByVal hWnd As LongPtr, _
               ByVal nIDEvent As LongPtr) _
          As Long
    
      Private lTimerIdUserForm As LongPtr
    
    #Else
    
      ' Returns an unsigned long pointer (nIDEvent) if the timer is created. Failure returns a zero.
      Private Declare Function SetTimer _
          Lib "User32.dll" _
              (ByVal HWnd As Long, _
               ByVal nIDEvent As Long, _
               ByVal uElapse As Long, _
               ByVal lpTimerFunc As Long) _
          As Long
    
      Private Declare Function KillTimer _
          Lib "User32.dll" _
              (ByVal HWnd As Long, _
               ByVal nIDEvent As Long) _
          As Long
    
      Private lTimerIdUserForm As Long
    
    #End If
    
     
    'Global 'Target Workbook Close' Date and Time
    Private xGblUserFormExpirationTimeInSeconds As Double
    
    'Timer Repeat Rate
    Private Const iTimerRepeatRateMILLISECONDS As Long = 500   '500 milliseconds = half of one second
    
    
    ''''''''''''''''''''''''''''''''''''''''''''''
    'MsgBox Message
    ''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''''
    'File Expiration 'Date and Time' Code
    ''''''''''''''''''''''''''''''''''''''''''''''
    Sub SetFileExpirationDateAndTime()
    
      'Put the 'Expiration Date and Time' in the cell
      'NOTE: Leading apostrophe formats the output as TEXT and not as a DATE
      Sheets("Sheet1").Range(sExpirationTimeADDRESS).Value = Format(myGblUserFormCloseDateAndTime, "hh:mm:ss")
            
    End Sub
    
    
    ''''''''''''''''''''''''''''''''''''''''''''''
    'UserForm Timer Code
    ''''''''''''''''''''''''''''''''''''''''''''''
    Sub DisplayUserForm1()
      'This opens the UserForm (in NonModal [access to Excel allowed] and starts the 'UserForm Timer'
    
      'Initialize the CountDown Timer Number of Seconds Expiration Date and Time
      '0.49 seconds added for 'Start Up' delay
      xGblUserFormExpirationTimeInSeconds = Timer + 120# + 0.49
      
      Call StopUserFormTimer
      Call StartUserFormTimer
      'UserForm1.Show vbModal      'Locks out Excel
      UserForm1.Show vbModeless   'Allows access to Excel
    
    End Sub
    
    Sub CloseUserForm1()
      'This stops the 'UserForm Timer' and closes the UserForm
      Call StopUserFormTimer
      Unload UserForm1
    End Sub
    
    
    Sub StartUserFormTimer()
      'This starts a 'UserForm Timer' that will repeat until stopped
      lTimerIdUserForm = SetTimer(0, 0, iTimerRepeatRateMILLISECONDS, AddressOf UserFormTimerEventHandler)
    End Sub
    
    Sub StopUserFormTimer()
      'This stops the 'UserForm Timer'
      KillTimer 0, lTimerIdUserForm
    End Sub
    
    Private Sub UserFormTimerEventHandler()
      'This is triggered each time the 'UserForm Timer' expires
      
      Dim xSecondsNow As Double
      Dim xSecondsRemaining As Double
      
      
      'Get the Current Date and Time
      xSecondsNow = Timer
    
      'Calculate the Number of Seconds Remaining
      'NOTE: Excel stores time as fractions of a day (e.g. 0.25 = 6 hours)
      xSecondsRemaining = xGblUserFormExpirationTimeInSeconds - xSecondsNow
      
      'Display the Time Remaining if the time remaining is Positive
      'Otherwise Stop the CountDown Timer and Close the UserForm
      If xSecondsRemaining > 0 Then
        'Display the Number of Seconds Remaining
        'Do not display a value less than 1 second
        If xSecondsRemaining < 1# Then
          xSecondsRemaining = 1#
        End If
        UserForm1.LabelCountDownTimer.Caption = Format(xSecondsRemaining, "0")
      Else
        'Stop the CountDown Timer and Close the UserForm
        Call CloseUserForm1
      End If
      
    End Sub
    It is a best practice to declare all variables. If you misspell a variable in your code, VBA will silently assume it is a Variant variable and go on executing with no clue to you that you have a bug. Go to the VBA development window, click Tools, Options, and check "Require Variable Declaration." This will insert the following line at the top of all new modules:
    Option Explicit
    This option requires all variables to be declared and will give a compiler error for undeclared variables.

    To access a Code Module in VBA:
    a. 'Left Click' on any cell in the Excel Spreadsheet.
    b. ALT-F11 to get to VBA.
    c. CTRL-R to get project explorer (if it isn't already showing).
    d. 'Double Click' on the module you want to access in the 'Project Explorer'.
    e. Insert code into the module if needed. 'Option Explicit' should only appear ONCE at the top of the module.

    Lewis

  3. #3
    Forum Contributor
    Join Date
    01-07-2012
    Location
    Bangalore
    MS-Off Ver
    Office 365
    Posts
    368

    Re: Countdown Timer on a Userform1

    Dear LJMetzger,

    Thank You So Much for your suggestions, It just Awesome!

    And the way you had explained also, its very crystal clear and gave me some confident to learn more on this VBA...

    Once again Thank You So Much!

    Regards,
    Rajeshkumar R

+ 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. Timer Countdown
    By Chris Wareham in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2011, 12:38 AM
  2. countdown timer
    By pka in forum Excel General
    Replies: 0
    Last Post: 10-23-2011, 09:18 AM
  3. Countdown timer
    By ukmxer in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-05-2007, 02:49 AM
  4. Countdown timer
    By Nemeo in forum Excel General
    Replies: 1
    Last Post: 05-24-2007, 09:37 PM
  5. Countdown timer
    By Nemeo in forum Excel General
    Replies: 0
    Last Post: 05-23-2007, 03:12 AM
  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