VBA Timer

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

VBA Timer

Post by VegasNath »

Code: Select all

Sub Timer()

Dim TimeBeg As Variant, TimeEnd As Variant
Dim Msg As String

TimeBeg = Format(Now(), "ddd dd mmm yyyy - hh:mm:ss")

'Do some other stuff

TimeEnd = Format(Now(), "ddd dd mmm yyyy - hh:mm:ss")

Msg = Msg & vbCrLf & vbCrLf & "Process Started - " & TimeBeg
Msg = Msg & vbCrLf & vbCrLf & "Process Completed - " & TimeEnd
Msg = Msg & vbCrLf & vbCrLf & "Completion Time = " & Format(TimeValue(Right(TimeEnd, 8)) - TimeValue(Right(TimeBeg, 8)), "hh:mm:ss")
        
MsgBox Msg, vbInformation

End Sub
Is there a way that I can have the message report in format "08 minutes & 09 seconds"?
:wales: Nathan :uk:
There's no place like home.....

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: VBA Timer

Post by agibsonsw »

I would prefer to set the variables as Date types so I can just subtract to find the time difference. Then use Format() when you want to display the results.

Code: Select all

Sub TimeThis()
    Dim TimeBeg As Date
    Dim TimeEnd As Date
    Dim TimeDiff As Date
    
    TimeBeg = Now
    MsgBox "Wait for this"
    TimeEnd = Now
    TimeDiff = TimeEnd - TimeBeg
    MsgBox Format(TimeDiff, "nn") & " minutes & " & Format(TimeDiff, "ss") & " seconds"
    
End Sub
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
VegasNath
5StarLounger
Posts: 1185
Joined: 24 Jan 2010, 12:02
Location: Wales, UK.

Re: VBA Timer

Post by VegasNath »

Thankyou, that works great. :cheers:
:wales: Nathan :uk:
There's no place like home.....