Solved

Excel 2010:  Start / Stop Timer Macro Button

Posted on 2013-06-17
5
5,474 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

729 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