Microsoft Excel - Calculate Hours Work - Taking in account overlap times

Asked By Tracy Baggett
09-Feb-12 04:07 PM
I need to calculate the total hours that each of my tutors actually work with students.  Some of the tutors work in a drop in lab, so some of the hours overlap.  How can I figure out total hours per day for each tutor?  For example, Drew Holland worked with 5 students on January 17, how can I determine the total time he worked with students - all the overlapping times?

[Consultants]FullName [Visits]Date In [Visits]Time In [Visits]Time Out [Students]Full Name
Cole, Jonathan 1/17/2012 12:28:11 13:28:11 Johnson, Cynthia D
Freeman, Parker 1/17/2012 15:57:58 16:57:58 Dortch, Althea
Freeman, Parker 1/17/2012 16:57:59 17:37:19 Scriven, Patrice R
Holland, Drew 1/17/2012 9:51:00 10:30:00 Lawrence, Christopher R
Holland, Drew 1/17/2012 10:46:00 12:42:00 Brooks, Trina J
Holland, Drew 1/17/2012 11:05:00 11:21:00 Sesay, Ishmael
Holland, Drew 1/17/2012 11:48:00 12:16:00 Jordan, Lenora J
Holland, Drew 1/17/2012 12:22:00 13:00:00 Royer, Charlene
Johnson, Xavier 1/17/2012 14:51:00 14:54:00 Estep, Tony G
McDonough, Carolyn 1/17/2012 9:08:01 9:25:53 Lewis, Denisha S
McDonough, Carolyn 1/17/2012 13:00:00 14:00:00 Royer, Charlene
McDonough, Carolyn 1/17/2012 14:08:06 14:44:19 Hogan, Linda R
McDonough, Carolyn 1/17/2012 14:53:17 15:51:25 Ortiz, Wilfredo
Patsiga, Robert 1/17/2012 9:54:00 10:55:00 Fields, Brittany L
Raye, Keith 1/17/2012 8:00:00 9:00:00 Blair, Everton A
Raye, Keith 1/17/2012 8:56:57 9:52:23 Hayes, Alberta D
Raye, Keith 1/17/2012 11:05:12 11:59:24 Harris, Adrienne L
  Pat Hartman replied to Tracy Baggett
09-Feb-12 10:10 PM

This is a very rudimentary piece of pseudo-code. I work with Access so I'm not sure how to work with excel recordsets. Hopefully you get the idea. Basically, you save the values from the previous row and compare them with values from the current row to determine when a "group" is complete. You didn't say what you wanted to do with the hours so I simulated writing a record for each name/date combination or you could just write them on another sheet.
 
Even if you were working in Access, which would certainly be a better tool for this job, you would still have to do this in a code loop. A query wouldn't work

.
The code is obviously untested but I believe the basic logic is sound.  It is imperative that the rows be sorted correctly so make sure you force the sort in the code.  Notice that when there is partial or full overlap, the saved values for in and out time don't both get saved.  If you have one record completely within the previous so that the end time of the second record is less than the first, if you replaced the end time, you could calculate double time for the third record.
Public Sub CalcHours()
Dim saveFullName As String
Dim saveInDate As Date
Dim saveInTime As Date
Dim saveOutTime As Date
Dim calcMinutes As Long
 
'sort the sheet by name, date in, time in, time out
'position the recordset to the first row
 
'fill variables with first set of data
  GoSub StartNewRec
   
  Do Until no more rows to process
    If FullName = saveFullName Then
      If InDate = saveInDate Then
        If InTime < saveOutTime Then ' overlap
          If OutTime < saveOutTime Then ' second range within prev range, no time to add Leave prev range
          Else
            calcMinutes = calcMinutes + DateDiff("m", saveOutTime, OutTime)
            saveOutTime = OutTime   'only replace outtime
          End If
        Else
          calcMinutes = calcMinutes + DateDiff("m", InTime, OutTime)
          saveInTime = InTime
          saveOutTime = OutTime
        End If
      Else
        GoSub WriteLastRec
        GoSub StartNewRec
      End If
    Else
      GoSub WriteLastRec
      GoSub StartNewRec
    End If
    move to next row
  Loop
     
         
  Exit Sub
StartNewRec:
  saveFullName = FullName
  saveInDate = InDate
  saveInTime = InTime
  saveOutTime = OutTime
  calcMinutes = DateDiff("m", InTime, OutTime)
Return
 
WriteLastRec:
  Write output record including name, date, calcminutes/60 to get hours
Return
 
End Sub

  R B replied to Tracy Baggett
10-Feb-12 01:44 AM

Hello,

 

[Consultants]FullName in Column  A,
[Visits]Date In in Column  B,

[Visits]Date In in Column C,

[Visits]Date In in Column D,

[Students]Full Name in Column E,

 

Now Store Diff in Column F and TotalHour in Column G

 

Step1:  Enter this in F2, and copy down as needed: (Difference two Time)

=D2-C2

Step2: Enter this in G1, and copy down as needed: (TotalTime)

=IF(A1<>A2,SUMIF($A$1:A1,A1,$F$1:F1),"")

This places total at *end* of group !

Set Format as Time in Column F

 

 

Check  working example in attached zip file

 

 Conditon.zip

 

 

Hope this is helpful !

Thanks

 

 

 

 

 

  Tracy Baggett replied to R B
10-Feb-12 08:44 AM
I want to subtract the overlapping times - I don't think I made that clear in my initial question.   So, Drew Holland's time would be less than the sum of each individual student due to overlapping times.  Does that make sense??
  Pichart Y. replied to Tracy Baggett
10-Feb-12 10:19 AM
Hi Tracy Baggett,

We can use array function...like this {=SUM((A2:A18=F2)*((D2:D18)-(C2:C18)))}

How does this function do...
  • from the 1st phase, (A2:A18=F2)
    • in this phase we check if any cell in A2:A18  = the value in F2 (here we select "Holland, Drew"
      • if any value in A2:A18 = Holland , Drew then it will return true, else will be false
    • in another group (D2:D18)-(C2:C18), this is end time - start time of each period...then we get the array of time spent by period...of all the row (2:18)
    • Then we multiply the time result with the first phase...then only true will give us time value, while false will give 0...(in excel formula true=1 , false=0)
    • Finally we sum all the time which match to the name we select in F2
  • Here is sample file...--->Fml_sumByCondition(Time).zip
  • Try...select the name in orange cell...
  • To create array function, input normal formula like this =SUM((A2:A18=F2)*((D2:D18)-(C2:C18)))}
    • Then instead of enter, hit ctrl+Shift+enter
    • you will get the{ } round your formula like this {=SUM((A2:A18=F2)*((D2:D18)-(C2:C18)))} 
Hope this help

Pichart Y.
  Pat Hartman replied to Pichart Y.
10-Feb-12 06:22 PM
Prichart,
The correct answer is 2 hours 53 minutes so your solution doesn't work either.  The earliest starting time is 9:51.  The latest ending time is 13:00.  That equals 3 hours and 9 minutes.  However there is a gap between 10:30 and 10:46 when Drew wasn't working so you need to subtract 16 minutes.  Ergo 2 hours 53 minutes.

Tracy,
If you don't care about non-working gaps, the simplest solution is to find the earliest in time and the latest out time and find the difference.  That will take much less code than my solution but you'll still need some.
  R B replied to Tracy Baggett
10-Feb-12 11:57 PM
Hello

in my solution,


I hv calculate each student different

after then sum this student by visitor.

is this wrong???

if you want other thing then let me clear
  kalpana aparnathi replied to Tracy Baggett
11-Feb-12 06:15 AM
hi,

Try this example code:

Dim rng As Range, DT As Range, Odates, p As Integer
Dim Hr As Integer
Dim Min As Double
Dim Thr As Double
Dim TT As Double
Dim oTime As Double, tTime As Double
Set rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
ReDim Odates(1 To rng.Count + 1, 1 To 2)
For Each DT In rng
  Min = Minute(DT.Offset(0, 1).Value) / 60
    Hr = Hour(DT.Offset(0, 1).Value)
     Thr = ((Min + Hr) / 24)
     TT = CDbl(DateValue(DT.Value)) + Thr
 DT.Offset(0, 5).Value = TT 'Rem
         p = p + 1
      Odates(p, 1) = TT
 
  Min = Minute(DT.Offset(0, 3).Value) / 60
     Hr = Hour(DT.Offset(0, 3).Value)
     Thr = ((Min + Hr) / 24)
      TT = CDbl(DateValue(DT.Offset(0, 2).Value)) + Thr
        Odates(p, 2) = TT
 DT.Offset(0, 6).Value = TT 'Rem
Next DT
'''''''''''''''''''''''''''
Dim EnDt As Double, ct As Integer
Dim Temp, Multi As Boolean
Multi = False
 
For ct = 1 To rng.Count
Temp = Odates(ct, 1)
  Multi = False
On Error Resume Next
 
Do While Odates(ct + 1, 1) > Temp _
  And Odates(ct + 1, 1) < Odates(ct, 2)
   
  If ct > rng.Count + 1 Then Exit Sub
    ct = ct + 1
     Multi = True
Loop
If Multi = True Then
  oTime = Odates(ct, 2) - Temp
    tTime = tTime + oTime
      rng.Cells(ct, 5).Value = oTime
Else
  oTime = Odates(ct, 2) - Temp
    rng.Cells(ct, 5).Value = oTime
      tTime = tTime + oTime
End If
Next ct
rng.Cells(rng.Count + 1, 5).NumberFormat = "0.00"
rng.Cells(rng.Count + 1, 5).Value = Format(tTime * 24, "0.00") & " Hrs"
MsgBox "Total Time Used equals " & Format(tTime * 24, "0.00") & " Hrs"
  Tracy Baggett replied to kalpana aparnathi
11-Feb-12 08:04 AM
How do I insert this code?  Thanks, Tracy
  Tracy Baggett replied to Pat Hartman
11-Feb-12 08:09 AM
Yes, the correct answer is 2:53 minutes!!  The data listed is a short list of one day.  I will do the calculation at the end of the month.  Therefore, tutors will have different dates & times.  No sure if that information needs to be taken into consideration.
  Tracy Baggett replied to Pat Hartman
11-Feb-12 08:18 AM
I don't know a lot about codes - but I tried creating a macro - I got this error message:

  Pat Hartman replied to Tracy Baggett
11-Feb-12 06:04 PM
Tracy,
You need to surround the code with a procedure header and footer. 
Public Sub calcHours()
  code goes here
End Sub

However, this code doesn't work either.   It seems to be "air" code even though  kalpana aparnathi didn't say that.  I had to correct a number of errors to get it to run.  Once I got it to run, it calculated 2.33 Hrs.  I think the problem is probably that the code is breaking up the fields into minutes and hours rather than simply using a datetime data type.

  Tracy Baggett replied to Tracy Baggett
12-Feb-12 02:09 PM
Should I send the complete file?  It will show different dates & times that the tutors worked.  I would need the excel sheet to give me total for each day, not a total for the whole month (subtracting any overlapping time).
  Pat Hartman replied to Tracy Baggett
12-Feb-12 07:09 PM
Tracy,
If you don't know how to code and you can't make use of the logic I provided, perhaps your employer would consider hiring someone to do this particular job.  You can't really expect someone to write the custom code you need for free.  It is one thing to provide advice and post snippets of sample code and even sample databases and quite another to deliver a complete custom solution.  If you need to write code for your job, you'll need to get training in VBA.  It isn't easy to find courses that will teach you how to program.  Try your local college.
Good Luck,
Pat
Create New Account
help
calculate moles in excel Excel HELP! How in Excel do I work out how many moles are in 1kg of water. Desperately need an answer for Monday morning. Excel Miscellaneous Discussions Microsoft Excel (1) Excel (1) JLatham (1) JerryI (1) Hypothesis (1) Loschmidt (1) Avogadro (1) Avagadro (1) Hi, So it's homework time is it? Firstly you can't do it solely in Excel without reference to a table of atomic weights and from that you should discover the
Counting rows that satisfy multiple conditions Excel Example: Excel Worksheet Discussions Microsoft Excel (1) Excel 2007 (1) SUMPRODUCT (1) ISNUMBER (1) AVERAGE (1) SUM (1) January (1) Equals (1) Hi source / xld.SUMPRODUCT.html And J.E. McGimpsey has some notes at: http: / / mcgimpsey.com / excel / formulae / doubleneg.html = = = = = = = = = = = = = = = = = = = And if you ever had to count the number of January's string. So, if you are looking for John it will be found in words like Johnson or Johnston or St. Johns. Being that your names are first names this wll probably not be an issue. - - Biff Microsoft Excel MVP Hmmm. I read this sentence: To mean that each cell in the range
create automatic response to questions in excel. . . . Excel Hi, Is there a way in excel where I have 10 questions and can click on one question and get the answer to pop up in my spreadsheet? thank you, Felicia Excel New Users Discussions Microsoft Excel (1) Intersect (1) Sheets (1) Worksheet (1) Workbook (1) Macros (1) Felicia (1) Dec (1 cell with a question the code produces a message box with the correct answer. Ken Johnson ok. . . sounds easy but where to i enter the code? After you copy the code paste to get the code in place. Then either go File | Close and Return to Microsoft Excel or use the key combination Ctrl + F11 to get back to the normal Excel
How is a Paretto chart created in Excel? Excel Do any of you Power-Users of Excel know how I can create a Paretto (sp?) chart within Excel? Excel Charting Discussions Microsoft Excel (1) Excel (1) Paretto (1) Pareto (1) Chem (1) Kim (1) Histogram (1) Toolpak (1) It's
adjust line spacing of text in a cell Excel How do I adjust the line spacing of text in a cell in Excel 2003? Excel Miscellaneous Discussions Microsoft Excel (1) Excel 2003 (1) Excel doesn't have that kind of word processing capability. - Jon - -- -- -- Jon Peltier Microsoft Excel MVP Tutorials and Custom Solutions Peltier Technical Services, Inc. - http: / / PeltierTech.com _ __ __ __ See