Consulting

Results 1 to 5 of 5

Thread: Timer & Decimal Places

  1. #1

    Cool Timer & Decimal Places

    Hi,I am using timer in a form I have to calculate how long a form is open for, so timer when the form is opened, and timer when a button is pressed. I then take the 2nd timer from the first to get the difference. However, it currently shows a few decimal places after the seconds, and ideally I want to get this down so it purely shows whole seconds. Any ideas if this is possible??Also, it would be good to be able to show minutes and seconds, if the number goes above 60, or minutes just as a 0 if this is not possible.Any help appreciated.
    We're a Kingdom, and we're United!!

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    I suppose it depends on the format you're dealing with.
    Here's an approach using the Date format[VBA]Dim datStart As Date

    Private Sub UserForm_Initialize()
    datStart = Now()
    End Sub

    Private Sub cmdStop_Click()

    Dim datEnd As Date
    Dim datDiff As Long
    Dim strResult As String

    datEnd = Now()
    datDiff = DateDiff("s", datStart, datEnd)
    If datDiff > 60 Then
    strResult = datDiff \ 60 & "m " & datDiff Mod 60 & "s"
    Else
    strResult = datDiff & "s"
    End If

    MsgBox strResult

    End Sub[/VBA]
    K :-)

  3. #3
    Excellent...thanks!
    We're a Kingdom, and we're United!!

  4. #4
    VBAX Regular
    Joined
    Mar 2006
    Posts
    44
    Location
    Or use Format(datDiff, "N:mm"), where N specifies minutes without a leading zero.

    Sean.

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I would use Timer

    [vba]
    Dim nTime As Double

    Private Sub UserForm_Initialize()
    nTime = Timer
    End Sub

    Private Sub cmdStop_Click()


    MsgBox Format(Timer - nTime, "0.0 secs")

    End Sub
    [/vba]

Posting Permissions

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