• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1323
  • Last Modified:

Excel formula required that calculates the total time from start to finish for each date period

I have a spreadsheet that is used to calculate different jobs that my staff do as chauffeurs.

What I would like is a formula that looks at the date column, then the start column for the time work started then finds the row that contains the last input of that date and the finish time and calculates the number of hours on the job.

The thing is that on the spreadsheet there are multiple dates, so it would need to search and find the start and finish time of each date on the spreadsheet and calculate the number of hours on the job for each date, showing the total amount of time on the job for that particular spreadsheet (or the work shown on the spreadsheet).  

The total amount of hours should be shown in the format of [h}:mm and be displayed in cell AB6.

One spreadsheet would normal have about 150 plus jobs on it, over a 15 day period.  So we would use about 150 rows starting from Row A13.

Also, please do not convert or save the spreadsheet in xlsx.  Most of my workers still use the xls format.

For privacy reasons I have remove the prices and Client Names from the spreadsheet.
1 Solution
Assuming that the dates and times are listed in ascending order, then you could use a worksheet formula like:
Results of this formula are a date/time serial number, and should be formatted as [h]:mm

This formula uses a user-defined function TotalHours, which is installed in Module1.
Function TotalHours(FirstDate As Double, LastDate As Double, Dates As Range, StartTimes As Range, FinishTimes As Range) As Double
'Returns the total number of hours worked between first start time on a day and last finish time, for every day between FirstDate and LastDate
Dim d As Double, dat As Double
Dim vStart As Variant, vFinish As Variant
Dim sDates As String, sFinish As String
sDates = Dates.Address(External:=True)
sFinish = FinishTimes.Address(External:=True)
For dat = FirstDate To LastDate
    Set vStart = Nothing
    Set vFinish = Nothing
    vStart = Application.Match(dat, Dates, 0)
    vFinish = Application.Evaluate("LOOKUP(2,IF(" & sDates & "=" & dat & "," & sFinish & ",""""))")
    If Not IsError(vStart) Then
        If Not IsError(vFinish) Then
            d = d + vFinish - StartTimes.Cells(vStart)
        End If
    End If
TotalHours = d
End Function

Open in new window

WorldsplaygroundAuthor Commented:
Absolutely great.  Works exactly the way I wanted it to.  You guys on Expert Exchange are sheer brilliant.  Best website and team of professionals I have ever come across.  Quick to respond and 9 out 10 times on the mark with success.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now