If the numbers are being entered as text then Excel would convert
TimeValue("06:30")
to 0.27 days which would be straightforward in converting to a differential
ie
Sub t()
timeinVAR = timesheet.timeIn.Value
timeoutVAR = timesheet.timeOut.Value
totalhoursvar = TimeValue(timeoutvar) - TimeValue(timeinvar)
If totalhoursvar < 0 Then
'Add 12 hours
totalhoursvar = 0.5 + totalhoursvar
End If
'Convert to days
ActiveCell.Value = totalhoursvar * 24
End Sub
So if your numbers could be entered as "HH:MM" it would be easy. If your users are entering the numbers as "HHMM" the string needs conversion like below
Excel now works in days so the end result is multiplied by 24 to get it back to hours
Sub t()
timeinVAR = timesheet.timeIn.Value
timeoutVAR = timesheet.timeOut.Value
'These functions are converted to time with TimeValue by inserting a ":"
newtimein = TimeValue(Left(timeinvar, Len(timeinvar) - 2) & ":" & (Right(timeinvar, 2)))
newtimeout = TimeValue(Left(timeoutvar,
totalhoursvar = newtimeout - newtimein
If totalhoursvar < 0 Then
totalhoursvar = 1 + totalhoursvar
End If
ActiveCell.Value = totalhoursvar * 24
End Sub
Main Topics
Browse All Topics





by: brettdjPosted on 2003-08-03 at 03:06:42ID: 9059171
Hi RascalBird,
This works by extracting the hour portion and then converting the minute portion back in %. But I think there should be a simpler approach with Time functions - I'll have a play
Sub test()
timeinVAR = timesheet.timeIn.Value
timeoutVAR = timesheet.timeOut.Value
If Len(timeinvar) = 4 Then
hourstimeinvar = Left(timeinvar, 2)
Else
hourstimeinvar = Left(timeinvar, 1)
End If
If Len(timeoutvar) = 4 Then
hourstimeoutvar = Left(timeoutvar, 2)
Else
hourstimeoutvar = Left(timeoutvar, 1)
End If
totalhoursVAR = hourstimeoutvar - hourstimeinvar
If totalhoursVAR < 0 Then
totalhoursVAR = 24 + totalhoursVAR
End If
ActiveCell.Value = totalhoursVAR + (Right(timeoutvar, 2) - Right(timeinvar, 2)) * 1 / 60
End Sub
Cheers
Dave