Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default difference between 2 times

How do I calculate the difference between 2 times. I have my code (I'm
beginner) so 1st input box asks for start time (e.g. 8:15) and 2nd inpu
box asks for endtime (e.g. 11:00) which should place the result of 2.7
in the activecell. My code is set up as follows.

Sub Timediff()
Dim Starttime As String
Dim Endtime As String

Starttime = InputBox("Enter Start Time")
Endtime = InputBox("enter Start Time")

hours = TimeValue(Endtime) - TimeValue(Starttime)
msgbox CInt(hours)

ActiveCell.Value = hours

End Sub

=======================================
My goal is to eventually build this into a worksheet that performs a
follows (using formulas) see attachment.



Thanks in Advanc

Attachment filename: time.xls
Download attachment: http://www.excelforum.com/attachment.php?postid=65181
--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default difference between 2 times

Do you really want your answer to display as a decimal or as correc
hours and mintues


this gives answer as hour and minutes

Sub Timediff()
Dim Starttime As String
Dim Endtime As String
Dim Hours As Date

Starttime = InputBox("Enter Start Time")

Endtime = InputBox("Enter End Time")

Hours = Format(TimeValue(Endtime) - TimeValue(Starttime), "hh:mm")

MsgBox Hour(Hours)

ActiveCell.Value = Hours

End Sub


If you want as decimal

then you will something like

ActiveCell.Value = Hour(Hours) & "." & (Minute(Hours) * 100) / 6

--
Message posted from http://www.ExcelForum.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default difference between 2 times

Thanks a lot. This helps out a lot and at least gives me something t
work with. However, when I enter a start time of 9:00 to say 1:15
will get a wrong answer unless I use a formula as in the attachmen
(unless I type in 13:15).

This is to track volunteer hours who generally come in in the mornin
and work anywhere from 3 to 6 hours so I would like it in decimal
(e.g. 3.75, 4.25).

Thanks

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default difference between 2 times

1:15 is shortly after midnight. 1:15 PM is shortly after noon. If you
want your code to always treat the second time as later than the first, you
can use an if statement

if endtime < starttime then endtime = endtime + timvalue("12:00")

- now do your calculation.

This assumes you won't have people working past midnight.

--
Regards,
Tom Ogilvy

"xlsxlsxls " wrote in message
...
Thanks a lot. This helps out a lot and at least gives me something to
work with. However, when I enter a start time of 9:00 to say 1:15 I
will get a wrong answer unless I use a formula as in the attachment
(unless I type in 13:15).

This is to track volunteer hours who generally come in in the morning
and work anywhere from 3 to 6 hours so I would like it in decimals
(e.g. 3.75, 4.25).

Thanks.


---
Message posted from http://www.ExcelForum.com/



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference Between Two Times ANYX Excel Discussion (Misc queries) 5 March 14th 09 12:24 AM
Difference between two times Jester Excel Discussion (Misc queries) 4 August 31st 06 01:36 PM
Calculating difference between two times Jaycatt Excel Worksheet Functions 2 August 8th 06 08:01 PM
Difference in Times bcbjork Excel Discussion (Misc queries) 6 August 27th 05 03:42 PM
Difference between 2 times and dates Stefan Buijs Excel Worksheet Functions 1 May 26th 05 02:21 PM


All times are GMT +1. The time now is 06:29 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"