Solved

Excel 2010:  Start / Stop Timer Macro Button

Posted on 2013-06-17
5
4,779 Views
1 Endorsement
Last Modified: 2014-10-22
Hello Experts!

    I'm needing a macro that consists of a start and stop button that will track time on each row in my spreadsheet.   The start and stop button will be in each row as shown below.  This allows the user to track true time and click on the start when the job actually begins and when it actually stops.  The Time Worked column will more than likely be Column L where the Start and Stop button will be in Column M.  Does anyone have a toggle or two button macro that can do this?  

                                                                  Time Worked                      (Button 1)   (Button 2)
data         data            data          data        00:12:23            <<<<<        START        STOP

data         data            data          data        00:04:54            <<<<<        START        STOP

data         data            data          data        01:12:04            <<<<<        START        STOP

and so on and so on....

Your help is greatly appreciated.
1
Comment
Question by:itsmevic
5 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39255223
Is this a duplicate of your previous question?
0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 39255238
The instructions below describe how to implement multiple independent timers on a worksheet. Each timer is continuously set to the elapsed time since that timer was started. Each timer is placed in a cell which can be formatted using any format.

This solution consists of code placed in a regular module and a class module. Place the code below in any regular module:

Private mTimers As Collection
Private mNextTime As Date

Public Sub UpdateTimers()

   Dim Timer As clsTimer

   If mTimers Is Nothing Then
      On Error Resume Next
      Application.OnTime mNextTime, "UpdateTimers", Schedule:=False
      On Error GoTo 0
      mNextTime = 0
   Else
      For Each Timer In mTimers
         Timer.Cell = (Now - Timer.StartTime)
      Next Timer
      mNextTime = Now + (1 - (Int(Now() * 86400) - Now() * 86400)) / 86400
      Application.OnTime mNextTime, "UpdateTimers"
   End If

End Sub

Public Sub StartTimer( _
      ByVal TargetCell As Range _
   )
   
   Dim Timer As clsTimer
   
   If mTimers Is Nothing Then
      Set mTimers = New Collection
   End If
   
   On Error Resume Next
   Set Timer = mTimers(TargetCell.Address)
   On Error GoTo 0
   
   If Timer Is Nothing Then
      Set Timer = New clsTimer
      Set Timer.Cell = TargetCell
      Timer.StartTime = Now
      mTimers.Add Timer, TargetCell.Address
   End If
   Timer.StartTime = Now
   If mNextTime = 0 Then
      UpdateTimers
   End If
   
End Sub

Public Sub StopTimer( _
      ByVal TargetCell As Range _
   )
   
   Dim Timer As clsTimer
   Dim Index As Long
   
   If mTimers Is Nothing Then
      Exit Sub
   End If
   
   On Error Resume Next
   Set Timer = mTimers(TargetCell.Address)
   On Error GoTo 0
   
   If Not Timer Is Nothing Then
      For Index = 1 To mTimers.Count
         If mTimers(Index).Cell.Address = TargetCell.Address Then
            mTimers.Remove (Index)
            Exit For
         End If
      Next Index
   End If
   
   If mTimers.Count = 0 Then
      Set mTimers = Nothing
   End If
   
End Sub

Create a new class module (Insert->Class Module), name the new module "clsTimer", and place the code below in the new module:

Public StartTime As Date
Public Cell As Range

To start a timer add the following statement to any routine such as a button click handler:

   StartTimer Sheets("Sheet1").[A1]

where the one parameter is the cell in which the timer is located.

Calling StartTimer again for an existing timer resets that timer to the current time. StartTimer can be called repeatedly an unlimited number of timers. All active timers are updated once a second. To stop a timer call StopTimer:

   StopTimer Sheets("Sheet1").[A1]

The timer cell can be formatted a number of different ways to display the elapsed time as described below.

Display total hours:

   [h]

Display total minutes:

   [m]

Display total seconds:

   [s]

Display hours, mimnutes, and seconds, e.g., "3:20:02":

   [h]:mm:ss

Display hours, minutes, and seconds with labels, e.g., "3 hours, 20 minutes, 2 seconds"

   [h] " hours," m "minutes," s "seconds"

Display total minutes and seconds:

   [m]:ss

Display total minutes and seconds with labels, e.g., "200 minutes, 2 seconds":

   [m] "minutes," s "seconds"

Kevin
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39255673
Much simpler to use a start and stop time column. ;)
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39255680
:-)
0
 

Expert Comment

by:Dan Green
ID: 40398415
Could someone post an example file that does this. I'm new to VBA (as in never touched it until today) and a full example file would be extremely helpful.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Outlook Free & Paid Tools
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now