MS Office Forum / Excel / Programming / March 2008
Time Difference in VBA Macro
|
|
Thread rating:  |
John Schneider - 03 Mar 2008 22:55 GMT I'm trying to write a VBA macro to take the difference between two times input by the user, calculate the difference, and put that value in the cell.
The first input box prompts for the start time (i.e. 9:00AM) and the second prompts for the end time (i.e. 5:30PM). I've searched through this newsgroup and done Google searches, but something just isn't sinking in. I need the value to come out as a decimal value. With the 9:00 - 5:30 example, the cell should show 8.5, but I keep getting -0.15. No doubt I'm missing something obvious!
Thanks, John
Bob Phillips - 03 Mar 2008 23:05 GMT Post up the code so we can see what you are doing.
 Signature --- HTH
Bob
(there's no email, no snail mail, but somewhere should be gmail in my addy)
> I'm trying to write a VBA macro to take the difference between two times > input by the user, calculate the difference, and put that value in the [quoted text clipped - 12 lines] > Thanks, > John John Schneider - 03 Mar 2008 23:26 GMT Its definately a work in progress, but here's what I have so far:
Sub testjs()
Dim startTime As String Dim endTime As String Dim totHours As String
startTime = Application.InputBox("Enter Start Time", "Calculate Hours", , , , , , 1) endTime = Application.InputBox("Enter End Time", "Calculate Hours", , , , , , 1)
totHours = CDate(endTime) - CDate(startTime)
ActiveCell.Value = totHours
End Sub
> Post up the code so we can see what you are doing. > [quoted text clipped - 14 lines] > > Thanks, > > John Billy Liddel - 04 Mar 2008 00:34 GMT Sub testjs()
Dim startTime As Date Dim endTime As Date Dim totHours As Date
startTime = Application.InputBox("Enter Start Time", "Calculate Hours", , , , , , 1) * 24 endTime = Application.InputBox("Enter End Time", "Calculate Hours", , , , , , 1) * 24
totHours = endTime - startTime ActiveCell.Value = totHours ActiveCell.NumberFormat = "00.00" End Sub
StartTime entered as 9:0 EndTime entered as 17:30
Is this OK?
> Its definately a work in progress, but here's what I have so far: > [quoted text clipped - 33 lines] > > > Thanks, > > > John John Schneider - 04 Mar 2008 18:16 GMT Billy,
That works perfect, but I don't understand why the time input (i.e. 9:00) has to be multiplied by 24.
Thanks again!!!
> Sub testjs() > [quoted text clipped - 54 lines] > > > > Thanks, > > > > John Billy Liddel - 05 Mar 2008 09:32 GMT John
Excel calculates dates as numbers, normally counting from 1/1/1900. Time is calculated as fractions of a day. so 9:00 is 9/24 of a day. Bear this in mind when making any calculations with time.
Peter
> Billy, > [quoted text clipped - 61 lines] > > > > > Thanks, > > > > > John John Schneider - 05 Mar 2008 15:10 GMT Great explanation, and thanks again for the help!!!!
> John > [quoted text clipped - 69 lines] > > > > > > Thanks, > > > > > > John John Schneider - 10 Mar 2008 22:54 GMT One more thing I can't figure out. The user inputs a start time, end time, and how long for lunch. Something is wrong with the way I'm trying to calculate this. The start and end times are absolute times (i.e. 8:30 am and 5:30 pm), but I need the "how long for lunch" to be just the number of minutes, i.e. 45, 60, etc. How do I get this to work?
Here's my code:
Dim startTime As Date Dim endTime As Date Dim Lunch As Date Dim totHours As Date
startTime = Application.InputBox("Enter Start Time", "Calculate Hours", , , , , , 1) endTime = Application.InputBox("Enter End Time", "Calculate Hours", , , , , , 1) Lunch = Application.InputBox("How Long For Lunch?", "Calculate Hours", , , , , , 1)
totHours = ((endTime - startTime) * 24) - (Lunch * 24)
ActiveCell.Value = totHours ActiveCell.NumberFormat = "#0.0"
> > John > > [quoted text clipped - 3 lines] > > > > Peter ward376 - 11 Mar 2008 01:04 GMT Divide the number of minutes by 1440 (24*60) to get a time serial equivalent to the number of minutes.
Cliff Edwards
|
|
|