PDA

View Full Version : Solved: Take time difference



Dreamer
05-01-2008, 04:43 PM
Dear all,

Please advise how to take the time difference of 2 dates...

e.g starttime is 1:45:00, endtime is 3:00:00,
how to calculate the difference in VBA and do the rounding?

endtime-starttime = duration =01:15, but i want to let it become "2 "(integer/value is preferred)?



Thanks so much..

rbrhodes
05-01-2008, 11:06 PM
Hi Dreamer,

Have a look...


Option Explicit
Sub TimesUp()

Dim TimeDiff As Long
Dim EndTime As Date
Dim StartTime As Date

StartTime = Range("A1")
EndTime = Range("B1")

'Give closest value
TimeDiff = (EndTime - StartTime) * 24

'Inform User
MsgBox (TimeDiff)

'OR
'Probably what you want, certainly what you asked for

'Round up to next Integer. ie 2 hrs 20 mins = 3
TimeDiff = Application.WorksheetFunction.RoundUp((EndTime - StartTime) * 24, 0)

'Inform User
MsgBox (TimeDiff)

End Sub

Bob Phillips
05-02-2008, 01:40 AM
You don't need VBA for something that trivial

=ROUNDUP((D1-C1)*24,0)