[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6075
  • Last Modified:

Excel 2010: Start / Stop Timer Macro Button

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
itsmevic
Asked:
itsmevic
1 Solution
 
Saqib Husain, SyedEngineerCommented:
Is this a duplicate of your previous question?
0
 
zorvek (Kevin Jones)ConsultantCommented:
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
 
Rory ArchibaldCommented:
Much simpler to use a start and stop time column. ;)
0
 
zorvek (Kevin Jones)ConsultantCommented:
:-)
0
 
Dan GreenCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now