Results 1 to 3 of 3

2 Stop Timers on User Form

Threaded View

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

    Re: 2 Stop Timers on User Form

    Hi VJOSHI,

    Try the following code, which is also in the attached file.

    There are global variables in the Ordinary Code module that store:
    a. If the specific timer is enabled
    b. The Start Time for that timer

    I changed the storage type to Double for the Timers, because that seems to be the data type that is usually used. My personal coding style is to put as little code in the UserForm module as possible, because it seems to be a lot easier for me to debug.

    My UserForm has the following control names that are used by the code:
    a. LabelTimer1 - Timer1 display
    b. LabelTimer2 - Timer2 display
    c. CommandButton1 - Starts Timer2
    d. CommandButton2 - Stops Timer2
    e. CommandButton3 - Resets Timer2


    UserForm1 module code:
    Option Explicit
    
    
    Private Sub UserForm_Activate()
      Call StartTimer1
      Call ProcessTimers
    End Sub
    
    Private Sub UserForm_Terminate()
      Call StopAllTimers
    End Sub
    
    Private Sub CommandButton1_Click()
      Call StartTimer2
    End Sub
    Private Sub CommandButton2_Click()
      Call StopTimer2
    End Sub
    Private Sub CommandButton3_Click()
      Call ResetTimer2
    End Sub

    Ordinary module code (such as Module1):
    Option Explicit
    
    Private bEnabledTimer1 As Boolean
    Private xStartTimeTimer1 As Double
    
    Private bEnabledTimer2 As Boolean
    Private xStartTimeTimer2 As Double
    
    Sub DisplayUserForm1()
      UserForm1.Show False
    End Sub
    
    Sub StartTimer1()
      bEnabledTimer1 = True
      xStartTimeTimer1 = Timer()
    End Sub
    
    Sub StartTimer2()
      bEnabledTimer2 = True
      xStartTimeTimer2 = Timer()
    End Sub
    
    Sub StopTimer2()
      bEnabledTimer2 = False
    End Sub
    
    Sub ResetTimer2()
      bEnabledTimer2 = False
      UserForm1.LabelTimer2.Caption = ""
    End Sub
    
    Sub StopAllTimers()
      bEnabledTimer1 = False
      Call StopTimer2
    End Sub
    
    
    Sub ProcessTimers()
    
      Dim bAtLeastOneTimerEnabled As Boolean
    
      Dim xElapsedTime As Double
      Dim LastEtime As Double
    
      bAtLeastOneTimerEnabled = True
    
      Do While bAtLeastOneTimerEnabled = True
      
        bAtLeastOneTimerEnabled = False
        
        ''''''''''''''''''''''''''''''''''''
        'Timer1 Processing
        ''''''''''''''''''''''''''''''''''''
        If bEnabledTimer1 = True Then
        
          bAtLeastOneTimerEnabled = True
      
          'Get the elapsed time
          'Allow Midnight rollover calculation
          xElapsedTime = Timer() - xStartTimeTimer1
          If xElapsedTime < 0# Then
            xElapsedTime = xElapsedTime + 86400#
          End If
      
          xElapsedTime = Int(xElapsedTime * 100) / 100
          UserForm1.LabelTimer1.Caption = Format(xElapsedTime / 86400, "hh:mm:ss:") & Format(xElapsedTime * 100 Mod 100, "00")
          DoEvents
         
        End If
        
        ''''''''''''''''''''''''''''''''''''
        'Timer2 Processing
        ''''''''''''''''''''''''''''''''''''
        If bEnabledTimer2 = True Then
        
          bAtLeastOneTimerEnabled = True
      
          'Get the elapsed time
          'Allow Midnight rollover calculation
          xElapsedTime = Timer() - xStartTimeTimer2
          If xElapsedTime < 0# Then
            xElapsedTime = xElapsedTime + 86400#
          End If
      
          xElapsedTime = Int(xElapsedTime * 100) / 100
          UserForm1.LabelTimer2.Caption = Format(xElapsedTime / 86400, "hh:mm:ss:") & Format(xElapsedTime * 100 Mod 100, "00")
          DoEvents
         
        End If
        
      Loop
      
    End Sub
    I hope this helps.

    Lewis
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] User Form to execute search and return all values to the user form for editing
    By allwrighty in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-11-2013, 10:40 PM
  2. [SOLVED] Excel vba user form- open directly to user form not worksheet
    By PANTECH in forum Excel Programming / VBA / Macros
    Replies: 26
    Last Post: 04-24-2013, 05:07 PM
  3. [SOLVED] Excel user form- If/Then statement outcome to show on user form
    By PANTECH in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-18-2013, 09:16 AM
  4. Userform - stop user saving or closing form is not complete
    By denise6372 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-18-2012, 12:24 AM
  5. calculating multiple stop/start timers within a cell
    By kmfdm515 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 01-24-2011, 02:20 PM

Tags for this Thread

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