[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Excel 2010:  Start / Stop Timer Macro Button

Posted on 2013-06-17
5
Medium Priority
?
5,828 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 2000 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

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.

Question has a verified solution.

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

Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

656 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