Combine sum up with timevalue function

Viewed 151

I would like to sum up the hours from this schedule. Cells are from C8 to H8.

example picture:
example picture

So far I have this code, where I used the timevalue function. This is set to give me the work hours for only C8 cell. How do I make it, so it would work like sum (C8: H8). So I get the sum of all the cells?

=TIMEVALUE(MID(C8,FIND("-",C8,1)+1, LEN(C8)-FIND("-",C8,1))) - TIMEVALUE(LEFT(C8,FIND("-",C8,1)-1))
4 Answers

I haven't heard your response on whether or not you're willing to use VBA, so I will go ahead and post a solution anyway. If you can't use it, perhaps someone else can in the future.

To get things started, press F12 while viewing your document to open the Save As dialog box. Under the filename, you should see another drop-down box "Save as Type:". Select Excel Macro-Enabled Workbook. Save your document.

enter image description here

Now open up the VBA Editor by simultaneously pressing the ALTF11 keys. To the left-side window is your Project Explorer. If you do not see this then press CTRLR. Right click the bold word that says VBAProject(WBName) > Insert > Module:

enter image description here

Next, we need to add a reference to the VBScript library. To do this, go to Tools > References. Then scroll down the list until you get to Microsoft VBScript Regular Expressions 5.5, then click the checkbox to the left and then 'OK'.

enter image description here

Now within the module you created earlier, copy and paste the following code:

Option Explicit

Public Function getHoursWorked(ByVal Rng As Range) As Date

    Dim cel As Range, runHours As Date
    With New VBScript_RegExp_55.RegExp
        .Pattern = "([\d:]+)-([\d:]+)"
        For Each cel In Rng.Cells
            If .Test(cel.Value) Then
                With .Execute(cel.Value)(0)
                    runHours = runHours + getDailyHours(.SubMatches(0), .SubMatches(1))
                End With
            End If
        Next cel
    End With
    
    getHoursWorked = runHours

End Function

Private Function getDailyHours(ByVal startTime As Date, ByVal endTime As Date) As Date
    getDailyHours = endTime - startTime
End Function

So now you're done with the hard part. Now you can close out of the above VBA Editor window and go use your newly created worksheet function: getHoursWorked()

In your cell, simply type =getHoursWorked(C8:H8) and it should return your value (you might need to change the format of the cell to [h]:mm if you haven't done so already.

enter image description here

You can include your formula in the SUMPRODUCT function and change reference from cell C8 to range C8:H8:

=SUMPRODUCT(TIMEVALUE(MID(C8:H8,FIND("-",C8:H8,1)+1,LEN(C8:H8)-FIND("-",C8:H8,1))) - TIMEVALUE(LEFT(C8:H8,FIND("-",C8:H8,1)-1)))

enter image description here

Edit:

For empty cells add IFERROR function and enter as array formula:

=SUMPRODUCT(IFERROR(TIMEVALUE(MID(C8:H8,FIND("-",C8:H8,1)+1,LEN(C8:H8)-FIND("-",C8:H8,1))) - TIMEVALUE(LEFT(C8:H8,FIND("-",C8:H8,1)-1)),0))

Array formula after editing is confirmed by pressing ctrl + shift + enter

In order to account for a time range spanning midnight, you may want to use this formula:

=SUM(MID(C1:H1,FIND("-",C1:H1)+1,10)-LEFT(C1:H1,FIND("-",C1:H1)-1),N(--MID(C1:H1,FIND("-",C1:H1)+1,5)<=--LEFT(C1:H1,FIND("-",C1:H1)-1)))

and custom format the result as [hh]:mm

The second argument to the SUM function adds 1 if the second time is earlier than the first, implying that the time period spanned midnight.

If you have any time spans that are greater than 24 hours, you will need to include the dates as well as the times.

enter image description here

To compensate for empty cells in the range, merely surround each calculation segment with IFERROR(....,0)

=SUM(
    IFERROR(
           MID(C1:H1,FIND("-",C1:H1)+1,10)
          -LEFT(C1:H1,FIND("-",C1:H1)-1),
    0),
    IFERROR(
        N(
            --MID(C1:H1,FIND("-",C1:H1)+1,5)<=
            --LEFT(C1:H1,FIND("-",C1:H1)-1)
        ),
    0)
)

You can use a summarize with the HOUR function for the formula you already extracted:

=SUM(HOUR(TIMEVALUE(MID(C8:G8,FIND("-",C8:G8,1)+1, LEN(C8:G8)-FIND("-",C8:G8,1))) - TIMEVALUE(LEFT(C8:G8,FIND("-",C8:G8,1)-1))))

or

=SUMPRODUCT(HOUR(TIMEVALUE(MID(C8:G8,FIND("-",C8:G8,1)+1, LEN(C8:G8)-FIND("-",C8:G8,1))) - TIMEVALUE(LEFT(C8:G8,FIND("-",C8:G8,1)-1))))

enter image description here

Related