Excel timespan calculation for time report - excel

I need to write a time report for my company.
Sadly I have to use a given format:
B1: 9.00 - 18.00 //timespan between arrival and leaving
C1: 60.00 //minutes I spent in drinking coffee
D1: 8.00 // total hours of work
I need a formula that a) calculates the total hours between the both times in A1, subtracts the minutes of having a break in minutes, and gives me the total hours worked in D1. I am not allowed to change the format of the cells (like writing arrival and departure times seperatly in columns) which makes it complicated.
Thank you in advance, Harry
UPDATE
=IF(ISBLANK(B16)," ",(TIMEVALUE(TRIM(RIGHT(B16, SEARCH("-",B16,1)-1)))-TIMEVALUE(TRIM(LEFT(B16, SEARCH("-",B16,1))))) * 24 -C16/60)
Works fine now.

You can try that for the result in D1:
=TRIM(RIGHT(B1, SEARCH(" - ",B1,1)))-TRIM(LEFT(B1,SEARCH(" - ",B1,1)-1))-C1/60
or if you don't have any spaces between the hyphen (-) I suggest:
TRIM(RIGHT(B1, SEARCH("-",B1,1)-1))-TRIM(LEFT(B1,SEARCH("-",B1,1)-1))-C1/60
Explanation:
=TRIM(RIGHT(B1, SEARCH(" - ",B1,1)-1)) = right part of the timespan (18.00)
=TRIM(LEFT(B1,SEARCH(" - ",B1,1)-1)) = left part of the timespan (9.00)
C1/60 = minute of having a break (1 hour = 60 minutes)

Related

calculate overtime within a time calculation and omit the regular time excel

trying to separate overtime and regular time into 3 cells. morning overtime, regular time, and after hours overtime in Excel. I go from site to site, and each work order gets its own row. I am subtracting end time from start time on each row. If a time goes through 08:00am, i.e. 06:00 - 10:00. then I need one cell calculation to say 2 hours overtime and the next cell calculation to say 2 hours of regular pay. same thing for the third cell except regular pay end at 5:00pm, i.e. 4:00pm to 8:00pm, 1 hours of regular pay and 3 hours of overtime.
I thought that I had it with =SUMPRODUCT, but computer crashed. been three weeks and I cannot get near to a solution. Also I need the the regular pay calculation to calculate only between 8:00am and 5:00pm no matter what is in the time calculation, sometimes I work 16 hours days
Here's a possible solution (tested with Office365) :
Example for line 2 :
E2 : conv.start = HOUR(C2)
F2 : conv.end = HOUR(D2)
G2 : working.time = F2-E2
H2 : morning.overtime = IF(AND(D2<8,E2<=8),E2-D2,IF(AND(D2<8,E2>8),8-D2,0))
I2 : after.hours.overtime = IF(AND(D2<17,E2>17),E2-17,IF(D2>17,E2-D2,0))
J2 : regular.time = E2-D2-(SUM(G2:H2))
J10 : total (regular.time) = SUM(J2:J9)*10.95

formating numbers into Time format

I want to convert numbers (that represent seconds) into HH:MM:SS format (like shown in pic..) but don't want to split them.. I want to format it using some formula or
If you just want to display the 715 seconds as hours:minutes:seconds, you will need a helper column that you use just for display.
You can create it either as in your example screenshot, or using a formula:
=TEXT(B2/86400,"[hh]:mm:ss")
Note the brackets around hh. That keeps the hours from rolling over every 24 and displays total hours correctly for instances where seconds > 86,400.
There's no way I know of to have 715 displayed as the associated hours:minutes:seconds in the same cell.
You can use a formula like this to convert 715 to HH:MM:SS:
=TEXT(FLOOR((A1-FLOOR(A1/86400,1)*86400)/3600, 1), "00:") & text(floor((A1 - floor(A1/3600,1)*3600)/60, 1),"00:") & text(A1 - floor(A1/3600,1)*3600 - floor((A1 - floor(A1/3600,1)*3600)/60, 1)*60,"00")
Let's break it down.
Get hours
A1 is seconds
There are 86400 seconds in a day
FLOOR(A1/86400, 1) gives us the floor of days
Multiply that by 86400 will give us seconds in those days FLOOR(A1/86400,1)*86400
Remove those many seconds from A1 and you get seconds remaining in the fraction of the last day A1- FLOOR(A1/86400,1)*86400
Divide those remaining seconds with 3600 to get hours FLOOR((A1- FLOOR(A1/86400,1)*86400 )/3600, 1)
Use TEXT function to format it to 2 digits and a colon at the end TEXT(FLOOR((A1- FLOOR(A1/86400,1)*86400 )/3600, 1), "00:")
Then, find remaining minutes and format it.
Then, take remaining seconds and format it.
Alternate method
Assume that your seconds are in column A6.
In B6, we will put Whole Days =FLOOR(A6/86400, 1)
In C6, we will put remaining seconds =A6-B6*86400
In D6, we will put Whole Hours =FLOOR(C6/3600,1)
In E6, we will put remaining seconds =C6-D6*3600
In F6, we will put Whole Minutes =FLOOR(E6/60, 1)
In G6, we will put remaining seconds =E6-F6*60
In H6, we'll format Hours, Minutes and Seconds =TEXT(D6,"00:") & TEXT(F6,"00:") & TEXT(G6,"00")

Excel Time Comparison and Subtraction

I am trying to do a time subtraction in excel of 30 minutes and I am running into a speed bump. So the table I have are as follows.
Table "Schedule"
Column 1 is day of the week (Mon-Sun) (formated as general, as this is plain text)
Column 2 is start time of the shift (formated as h:mm AM/PM)
Column 3 is end time of the shift (formated as h:mm AM/PM)
Column 4 is duration of the shift (start to end) (formated by formula (TEXT(col3-col2,"h:mm")) )
Column 5 is paid hours (if the total hours is over 6.5 then subtract 0.5 hours for an unpaid lunch) (formula IF(col5>"6:30",col5-"0:30",D5) )
The issue is any time allotment over 10 hours start to end (where column 4, the duration hits 10 hours) no lunch is subtracted at all.
So...
Start 9:00 AM, End 6:59 PM, Hours Total 9:59, Hours Paid 9:29
But...
Start 9:00 AM, End 7:00 PM, Hours Total 10:00, Hours Paid 10:00
and that should obviously not happen. I can't find anything on google so I figured the excel gurus here may have some advice.
Thanks!
If your time columns are stores using excel's dedicated time format, this should be straightforward. Mixed data types are likely your problem.
First, be sure your time columns (columns 2 and 3) are set using the time function, i.e.,
=time(hours,minutes,seconds)
Then, you should be able to add and subtract easily.
Column 4: = column 3 - column 2
... then subtract 30 minutes also using the time() function:
Column 5: = if(column 4 > time(6,30,0),column 4 -time(0,30,0),column 4)
Excel stores time values from 0 to 1. So 24 hours=1, 12 hours=.5 etc. That means 6.5 hours=0.270833333 and .5 hours=0.020833333. As a result you can just do a simple if statement.
=IF(D2>0.270833333,D2-0.020833333,D2)
To turn it into a time format, is to just use excel's time formating options.

calculate cost from hours worked

I have two columns in my spreadsheet.
A B
Total Hours Worked Total Cost
A is formatted as a duration/time, i.e. 01:34:20 and is the total elapsed time it's taken to complete a task
B should be a formula of some sort that calculates the cost. Right now I have the function A2*25 but this results in B2 being too little.
What do I need to do to correct the formula so the total cost is correct?
You can use this as B2 -
=(HOUR(A2) + MINUTE(A2) / 60 + SECOND(A2) / 3600)*25
Excel stores durations as fractions of days.
You just need to multiply the duration by 24 to have the number of hours worked and you get the correct result.
=A2*25*24

How to count hours in excel

I have xls file in following format
Name 1 2 3 4
John 09:00-21:00 09:00-21:00
Amy 21:00-09:00 09:00-21:00
Where 1,2,3,4 and so on represent days of current month,
09:00-21:00 - working hours.
I want to calculate salary based on the following conditions:
09:00-21:00 - 10$/hour
21:00-00:00 - 15$/hour
00:00-03:00 - 20$/hour
etc.
and so on (every hour can have it's own cost, for example 03:00-04:00 - 20$/hour, 04:00-05:00 - 19$/hour, etc.)
How can i accomplish this using only Excel (functions or VBA)?
P.S. Easy way: export to csv and process in python/php/etc.
Here is a non-VBA solution. It's a pretty nasty formula, but it works. I am sure it could be made even easier to use and understand with some more ingenuity:
Assuming the spreadsheet is set up like this:
Enter this formula in cell G1 and drag down for your data set:
=IF(ISBLANK(B2),"",IF(LEFT(B2,2)<MID(B2,FIND("-",B2)+1,2),SUMIFS($P$2:$P$24,$Q$2:$Q$24,">="&LEFT(B2,2),$Q$2:$Q$24,"<="&MID(B2,FIND("-",B2)+1,2)),SUMIF($Q$2:$Q$24,"<="&MID(B2,FIND("-",B2)+1,2),$P$2:$P$24)+SUMIF($Q$2:$Q$24,">="&LEFT(B2,2),$P$2:$P$24)))
To explain the formula in detail:
IF(ISBLANK(B2),"" will return a empty string if there is no time for a given person / day combination.
LEFT(B2,2) extracts the start-time into an hour.
Mid(B2,Find("-",B2)+1,2) extracts the end-time into an hour.
IF(LEFT(B2,2)<MID(B2,FIND("-",B2)+1,2) will check if the start-time is less than the end-time (meaning no over-night work). If the start-time is less than the end-time, it will use this formula to calculate the total cost per hour: SUMIFS($P$2:$P$24,$Q$2:$Q$24,">="&LEFT(B3,2),$Q$2:$Q$24,"<="&MID(B3,FIND("-",B3)+1,2))
If the start-time is higher than the end-time (meaning overnight work), it will use this formula to calculate: SUMIF($Q$2:$Q$24,"<="&MID(B3,FIND("-",B3)+1,2),$P$2:$P$24)+SUMIF($Q$2:$Q$24,">="&LEFT(B3,2),$P$2:$P$24)
The use of the Find("-",[cell]) splits the start-and- end times into values excel can use to do math against the Time / Cost table.
The formula in column Q of the Time / Cost table is =VALUE(MID(O2,FIND("-",O2)+1,2)) and turns the ending hour to consider the cost into a value Excel can use to add, instead of having the text from your original source format.
Do this in VBA! It is native to excel and is easy to learn. Functionally, I would loop through the table, write a function to calculate the dollars earned based on the info given. If you want your results to be live updating (like a formula in excel) you can write a user defined function. A helpful function might be an HoursIntersect function, as below:
Public Function HoursIntersect(Period1Start As Date, Period1End As Date, _
Period2Start As Date, Period2End As Date) _
As Double
Dim result As Double
' Check if the ends are greater than the starts. If they are, assume we are rolling over to
' a new day
If Period1End < Period1Start Then Period1End = Period1End + 1
If Period2End < Period2Start Then Period2End = Period2End + 1
With WorksheetFunction
result = .Min(Period1End, Period2End) - .Max(Period1Start, Period2Start)
HoursIntersect = .Max(result, 0) * 24
End With
End Function
Then you can determine the start and end time by splitting the value on the "-" character. Then multiply each payment schedule by the hours worked within that time:
DollarsEarned = DollarsEarned + 20 * HoursIntersect(StartTime, EndTime, #00:00:00#, #03:00:00#)
DollarsEarned = DollarsEarned + 10 * HoursIntersect(StartTime, EndTime, #09:00:00#, #21:00:00#)
DollarsEarned = DollarsEarned + 15 * HoursIntersect(StartTime, EndTime, #21:00:00#, #00:00:00#)
I have a method that uses nothing but formulas. First create a lookup table which contains every hour and rate in say columns K & L, something like this:
K L
08:00 15
09:00 10
10:00 10
11:00 10
12:00 10
13:00 10
14:00 10
15:00 10
16:00 10
17:00 10
18:00 10
19:00 10
20:00 10
21:00 15
22:00 15
23:00 15
Make sure you enter the hours as text by entering a single quote before the digits.
Then if your hours were in cell B2 you could then use this formula to calculate the total:
=SUM(INDIRECT("L"&MATCH(LEFT(B2,5),K2:K40,0)&":L"&MATCH(RIGHT(B2,5),K2:K40,0)))
All the formula is doing is getting the left and right text of your work time, using MATCH to find their positions in the lookup table which is used to create a range address which is then passed to SUM via the INDIRECT function.
If you need to worry about minutes all you need to do is create a bigger lookup table which holds every minute of the day. You may need to add some extra logic if your work days span midnight.

Resources