ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Help with Time Calculations (https://www.excelbanter.com/excel-programming/313473-help-time-calculations.html)

Don

Help with Time Calculations
 
On my worksheet, my user is entering start and stop times for employees and
then in a separate cell, displaying the hours worked. I have been using the
hour(b1-a1)+hour(d1-c1), etc. cell functions to calculate the hours. But
now, the times are changing and I would like to use the code to calculate
the hours. The below is what I have, but it doesn't calculate the hours
correctly......I've used 7:30am and 4pm in cells a1 thru j1......The result
it gives me is 18:30 and it should be 40:00......What am I doing wrong?

Dim daystart, dayend, dailyhrs, weekhrs As Date
For i = 0 To 6
daystart = ActiveCell.Value
ActiveCell.Cells(1, 2).Activate
dayend = ActiveCell.Value
dailyhrs = dayend - daystart
weekhrs = weekhrs + dailyhrs
ActiveCell.Cells(1, 2).Activate
Next i
Range("a3").Select
ActiveCell.FormulaR1C1 = weekhrs

Thanks,
Don



Frank Kabel

Help with Time Calculations
 
Hi
format your target cell with the custom format
[hh]:mm

--
Regards
Frank Kabel
Frankfurt, Germany


Don wrote:
On my worksheet, my user is entering start and stop times for
employees and then in a separate cell, displaying the hours worked. I
have been using the hour(b1-a1)+hour(d1-c1), etc. cell functions to
calculate the hours. But now, the times are changing and I would like
to use the code to calculate the hours. The below is what I have, but
it doesn't calculate the hours correctly......I've used 7:30am and
4pm in cells a1 thru j1......The result it gives me is 18:30 and it
should be 40:00......What am I doing wrong?

Dim daystart, dayend, dailyhrs, weekhrs As Date
For i = 0 To 6
daystart = ActiveCell.Value
ActiveCell.Cells(1, 2).Activate
dayend = ActiveCell.Value
dailyhrs = dayend - daystart
weekhrs = weekhrs + dailyhrs
ActiveCell.Cells(1, 2).Activate
Next i
Range("a3").Select
ActiveCell.FormulaR1C1 = weekhrs

Thanks,
Don




All times are GMT +1. The time now is 04:11 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com