Search code examples
libreoffice-calc

Calculate the amount of hours Libreoffice calc formula


Hi I would like to calculate the amount of hours between few pairs of time where ';' is separator. I know how to do it by spliting it but I would like to avoid this and do it in one cell. For example from 8:00-12:00;13:30-16:00;16:30-17:15 cell should display 7,25 or 7:15. Thanks for your answers.


Solution

  • Something like this

    Function Total_Time(sList As String) As String 
    Dim aTemp As Variant, aPair As Variant
    Dim i As Long, dRes As Double 
        aTemp = Split(sList, ";")
        For i = LBound(aTemp) To UBound(aTemp)
            aPair = Split(aTemp(i), "-")
            dRes = dRes + TimeValue(aPair(1))
            dRes = dRes - TimeValue(aPair(0))
        Next i
        Total_Time = Format(dRes,"HH:MM")
    End Function