Search text column for certain strings and return the time in the cell, Excel - excel

I work in a hospital and need to keep track of how much time and when I spend that time in different areas. Clinical and Research. There are multiple things that count as clinical hours though they are not the same thing. In the picture CFW, Clinic, AMJ, etc count as clinical. Training, Research, lunch, data, r383, experiment are not needed to keep track of. It appears like this:
___ A ____ B
1 8:30 Training
2 9:00 CFW
3 9:30 CFW
4 10:00 CFW
5 10:30 Clinic
6 11:00 Clinic
7 11:30 Research
8 12:00 Lunch
9 12:30 Data
10 1:00 AMJ
11 1:30 AMJ
12 2:00 AMJ
13 2:30 R383
14 3:00 Experiment
15 3:30 Experiment
16 4:00 Research
My end goal is to be able to have a list of text that I can search for, like "Clinic", and it tell me which times I was there and the total time spent, like 10:30-11:30 1 hour.
Clinic: 10:30-11:30
1 Hour
So if I search all of the text phrases it would have the times spent there (beginning - end), and the sum total length of time. I have a separate sheet for each day of the week and ideally would like to be able to search through all 5 sheets at once and have all the days pop up all organized and such.
For example:
Monday
Clinic: 10:30-11:30
1 Hour
AMJ: 1:00-2:30
1.5 Hours
Tuesday
CFW: 9:00-10:30
1.5 Hours
etc...
Sorry for my formatting. Let me know if I need to any more information. I'm not able to post the picture yet. Any ideas? Please and thank

You want to create a VBA Macro on your Excel Spreadsheet that will go through each sheet, look for what value you want (e.g. "Clinic"), capture your times and calculate the elapsed.
You can refer to each sheet, and the cells in loops. Here is a shell to get you started.
Public Function elapsedTime (byval searchFor As String)
Dim shIdx as Integer
Dim rIdx as Integer
Dim calc as Integer
rIdx = 2
For shIdx = 1 to 5
While Sheets(shIdx).Range("A" + CStr(rIdx)).Value <> ""
If Sheets(shIdx).Range("B" + CStr(rIdx)).Value = searchFor Then
Additional logic here to calculate your value and
store it in "calc"...
End If
rIdx = rIdx + 1
Wend
rIdx = 2
Next shIdx
elapsedTime = calc
End Function

Related

Count values of an Excel column in different ranges

I want to count number of values in different ranges in an Excel column.
Example 1:
Imagine I have some data in 40 rows, each one happened in different time of day. Like the below image:
now I want to count number of row repeated in different ranges, for example, count number of rows that have time between 12:00 to 18:00, again count 18:00 to 00:00, and more to 11:59 (next 12:00)
Time range
Count
00:00 to 6:00
?
06:00 to 12:00
?
12:00 to 18:00
?
18:00 to 23:59
?
Finally I have a table with 4 rows that shows how many row I have in those ranges and I can create a chart by that.
Example 2:
Count people based on age range. result would be like this:
Age range
Count
12 to 18
3
18 to 25
5
25 to 35
4
35 to 45
1
45 to 60
2
P.S:
I used countif with logical AND, but it didn't work. like this: =COUNTIFS(C:C,"AND(<00:00, >2:00)")
A more correct use of COUNTIFS (which is different from COUNTIF), would be :
'Counts values strictly between 00:00 and 2:00
=COUNTIFS(C:C,">00:00",C:C,"<2:00")
Hope it helps

Allocating staff to 30 minute intervals

I’m trying to create either a spreadsheet or macro that allows me to put and employee start and finish time in and then show me by each half hour how many half hours they are in.
For example;
Employee 1 - 08:30 to 10:15
The output I would want is;
08:00 0
08:30 1
09:00 1
09:30 1
10:00 0.5
10:30 0
11:00 0
So between 8:30 and 9:00 shows as one because that employee was in during that half hour. But between 10:00 and 10:30 it shows 0.5 because the employee was only in for half of that period
Then as I add more data it tells me which half hours they are covering.
I’ve tried using the ceiling and floor values, counta, countif, countifs, v and hlookups but just can’t get the desired effect.
I’m guessing it would need to be a macro.
Assuming C1 and D1 contain the start and end times of a shift and column A contains the times, use the following formula in column B:
=IF(A2>D$1,C2,IF(AND(A2>=C$1,A3<=D$1),1,MAX(0,A3-D$1)/(1/48)))
Limitations: shifts cannot cross over days, days cannot start before 00:00 or end after 23:59

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.

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.

Derive an End date by Man hours and a start date in Excel-2010

I need a formula which calculates the End Date (with Time) when total Man Hours and Start Date are given. [Excel 2010]
Criteria:
1 Man Day = 8 Hours (1 hour is break time)
Work Start Time = 10:00
Work End Time = 19:00
1 Man Week = Mon - Fri
Holidays, if any, should not be counted
For example:
Cell E7 = Man Hours = 10 Hrs
Cell F7 = Start Date = 26-Jun-15 13:00
Cell G7 = End Date = ???? (Ideally 29-Jun-15 14:00)
*27th and 28th are weekends
Thank you!
margin of error for the estimate of duration from estimate of effort is too big for an hourly granularity to make any sense as long as real people and realistic plans are concerned
however, if we have an estimate of duration in working hours on the input (as opposed to calendar days/weeks/months which are more common units of duration estimates) and we need to calculate the end date-time, we could:
compute helper column with the would-be end hour if there were no closing hours (e.g. in cell H7, in Excel Date units = fractions of a day)
=MOD(F7, 1) + MOD(E7, 8)/24
check if the would-be hour is before the closing hours and adjust the end day + end time accordingly. If it's after, move to the next workday and adjust time, e.g. for simplicity assume all breaks are at the beginning of a day, so we can subtract the number of hours in a workday:
=IF(H7 <= 19/24,
WORKDAY(F7, E7/8, holidays) + H7,
WORKDAY(F7, E7/8 + 1, holidays) + H7 - 8/24)
assume you saved your national holidays on column I, you can try below in cell H7:
=IF(HOUR(F7+E7/24)>19,((F7+E7/24)-INT(F7+E7/24)-0.792)+(WORKDAY(F7,1,I:I))+0.417,F7+E7/24)

Resources