Consulting

Results 1 to 5 of 5

Thread: Calculating Duration

  1. #1

    Cool Calculating Duration

    Hi,

    I am using the below code to try and work out the duration between 2 times automatically, but I just keep getting 0 as the duration

    [VBA]
    Start = Now
    Finish = Now
    Duration = Finish - Start
    [/VBA]

    Any ideas why this might be?? I'm thinking possibly becuase of the formatting, but not sure...and if this is the case, not sure how to fix

    Thanks in advance for any help.
    We're a Kingdom, and we're United!!

  2. #2
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    [VBA]Sub TimeIt()

    Dim Start As Double, Finish As Double

    Start = Timer
    '--------------------------------------

    'Your code to be timed here

    '--------------------------------------
    Finish = Timer

    MsgBox " run time was " & Finish - Start

    End Sub[/VBA]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    Many thanks!

    And how do I format this?? I tried changing the format to h:mm:ss and expected the result 0:00:03 on a test that I did, but 7:35:37 was displayed.

    This is my full code:
    [vba]
    Private Sub CommandButton1_Click()
    If CommandButton1.Caption = "Start" Then
    Start = Timer
    CommandButton1.Caption = "Finish"
    Range("A65000").End(xlUp).Offset(1, 0).Value = Now
    Else
    Range("A65000").End(xlUp).Offset(0, 1).Value = Now
    Finish = Timer
    Range("A65000").End(xlUp).Offset(0, 2).Value = Finish - Start
    Answer = InputBox("Please enter a general description of the " & _
    "task you have just completed", "Task Information")
    Range("A65000").End(xlUp).Offset(0, 3).Value = Answer
    CommandButton1.Caption = "Start"
    End If
    End Sub
    [/vba]
    We're a Kingdom, and we're United!!

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Dowsey.
    Your start time is not being "remembered" by the code. Try setting A1 to Timer at Start and Timer - A1 at Finish
    Regards
    MD
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Since you want a time difference, you might have to format it using the square brackets: [hh]:mm:ss

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •