+ Reply to Thread
Results 1 to 9 of 9

Can someone help me create a stopwatch?

Hybrid View

  1. #1
    Registered User
    Join Date
    06-11-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2007
    Posts
    86

    Re: Can someone help me create a stopwatch?

    http://www.excelforum.com/excel-prog...tch-timer.html

  2. #2
    Valued Forum Contributor WasWodge's Avatar
    Join Date
    08-02-2010
    Location
    Hampshire,England
    MS-Off Ver
    Office 365 and Office 2010
    Posts
    882

    Re: Can someone help me create a stopwatch?

    I can't see that Mike's code doesn't do exactly as requested but here is another post on the forum where the code could be adapted (see sheet in post #1).

    http://www.excelforum.com/excel-prog...bike-laps.html
    If my solution worked (or not) please let me know. If your question is answered then please remember to mark it solved

    Computers are like air conditioners. They work fine until you start opening windows. ~Author Unknown

  3. #3
    Registered User
    Join Date
    06-11-2012
    Location
    Shreveport, Louisiana
    MS-Off Ver
    Excel 2011
    Posts
    7

    Re: Can someone help me create a stopwatch?

    Thanks.

    I'm going to try out all your suggestions today. I'll post back after.

    -D

  4. #4
    Registered User
    Join Date
    06-11-2012
    Location
    Shreveport, Louisiana
    MS-Off Ver
    Excel 2011
    Posts
    7

    Re: Can someone help me create a stopwatch?

    Hi mikerickson

    You code is working really well. The only thing I'd like to alter is the cells in which the times get listed A1-C1 and A2-C2 and so on.

    Instead of the the stop watch moving through the range of A-C. I'd like the stop watch to start in B23 and then list through B23, C23, D23 and then B24, C24, D24 and so on.

    I tried playing around with the offsets and LastCellInA... but I didn't achieve anything other than moving around the interior color. Am going to keep playing with it.

    Any suggestions?

    Thank you.

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Can someone help me create a stopwatch?

    This should work. If you change your mind about B23, just change the indicated line in Function LastCellInColumn.
    Function LastCellInColumn(Optional ReturnBase As Boolean)
        Dim startCell As Range
        With ThisWorkbook.Sheets("Sheet1")
        
            Set startCell = .Range("B23"): Rem adjust <<<<<<<<<<<<<<<<<<<<<
            
            Set LastCellInColumn = .Cells(.Rows.Count, startCell.Column).End(xlUp)
        End With
        If ReturnBase Or (LastCellInColumn.Row < startCell.Row) Then Set LastCellInColumn = startCell
    End Function
    
    Sub StartButton()
        Rem set named value StartTime to Now
        ThisWorkbook.Names.Add Name:="StartTime", RefersTo:="=" & CDbl(Now())
        Rem mark that timer is on
        LastCellInColumn.Interior.Color = vbYellow
    End Sub
    
    Sub StopButton()
        Dim StartTime As Date
        On Error Resume Next
            StartTime = CDate(Evaluate(Names("StartTime").RefersTo))
        On Error GoTo 0
        
        If 0 < StartTime Then
            Rem if timer is running
            With LastCellInColumn
                .Interior.ColorIndex = xlNone
                Rem write interval
                With .Offset(0, 2)
                    .Value = Now() - StartTime
                    .NumberFormat = "h:mm:ss"
                End With
                
                Rem calculate adjusted start time
                With .Offset(0, 1)
                    .Value = .Offset(0, 1).Value + .Offset(0, -1).Value
                    .NumberFormat = "h:mm:ss"
                End With
                
                Rem prepare next row
                With .Offset(1, 0)
                    .Value = .Offset(-1, 1).Value
                    .NumberFormat = "h:mm:ss"
                End With
            End With
            
            Rem turn off timer by setting name StartTime to 0
            ThisWorkbook.Names.Add Name:="StartTime", RefersTo:="=0"
        End If
    End Sub
    
    Sub MarkButton()
        If ThisWorkbook.Names("StartTime").RefersTo <> "=0" Then
            Call StopButton
            Call StartButton
        End If
    End Sub
    
    Sub ClearButton()
        Dim strPrompt As String
        If ThisWorkbook.Names("StartTime").RefersTo <> "=0" Then
            strPrompt = "The timer is running" & vbCr & vbCr
        End If
        strPrompt = strPrompt & "Clear data?" & vbCr & "There is no un-do."
        
        If MsgBox(strPrompt, vbYesNo) = vbYes Then
            Call StopButton
            
            Range(LastCellInColumn, LastCellInColumn(True)).Resize(, 3).ClearContents
                
            With LastCellInColumn
                .Resize(1, 3).Value = Split("Start Time,End Time,Duration", ",")
                .Offset(1, 0).Value = 0
                .Offset(1, 0).NumberFormat = "h:mm:ss"
            End With
        End If
    End Sub
    Last edited by mikerickson; 06-16-2012 at 05:51 PM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  6. #6
    Registered User
    Join Date
    06-11-2012
    Location
    Shreveport, Louisiana
    MS-Off Ver
    Excel 2011
    Posts
    7

    Re: Can someone help me create a stopwatch?

    mikerickson... I don't know who you are- but you're a hero in my book!

    Thanks a lot for the code. It works perfectly.

    -D

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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