Sum a custom function

Gregorys05

Board Regular
Joined
Sep 24, 2008
Messages
217
Hi guys,
I have the below function that calculates time in hours and mins if you type in seconds.

e.g 1200 secs - formula : =calctime(1200) cell value: 20:00

The problem i have is that if i have two columns with the formula in that i want to sum i cannot do this by just using the sum function.

Any ideas how i can do this at all??

Thanks

Code:
Function CalcTime(Seconds) As String
    Dim tData1, tData2
    If Not IsNumeric(Seconds) Then Exit Function
    If Seconds < 60 Then
        If Len(Seconds) = 1 Then
            CalcTime = "00:0" & Seconds
        Else
            CalcTime = "00:" & Seconds
        End If
    Else
        tData1 = RoundDown(Seconds / 60)
        tData2 = Seconds - tData1 * 60
        
        If Len(tData1) = 1 Then
            tData1 = "0" & tData1
        End If
        
        If Len(tData2) = 1 Then
            tData2 = "0" & tData2
        End If
        
        CalcTime = tData1 & ":" & tData2
    
    End If
End Function
Function RoundDown(Number)
    Dim tData1
    tData1 = InStr(Number, ".")
    If tData1 = 0 Then
        RoundDown = Number
    Else
        RoundDown = Left(Number, tData1 - 1)
    End If
End Function
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Your function returns strings, not actual time values.

Try changing your function to something like:
Code:
Function SecToTime(Seconds As String) As Date
Dim OneSec As Date
OneSec = CDate("0:00:01")
SecToTime = Val(Seconds) * OneSec
End Function
 
Upvote 0
Do you really need a custom function? Could you not just use a standard formula like
=A2/1440 (where A2 holds your seconds - or did you really mean the first number is minutes? 1200 seconds = 20 minutes or 1200 minutes = 20 hours) and custom format the cell as "[hh]:mm" and then just use the normal SUM() function?

Excel Workbook
ABCDEF
1Time 1Time 2Time 1Time 2Sum
2606701:0001:0702:07
3752001:1500:2001:35
4210000:0201:4001:42
51200120020:0020:0040:00
SUM
 
Last edited:
Upvote 0
Misca, that helps thank you, the only problem i have is that now if the value goes over 1 day 1440 then the calcution come back with #Value!

Any ideas
 
Upvote 0
Do you need an array formula? Wouldn't this (with appropriate cell references) do it?
=H2+I2

Mind you, I still cant see the need for a custom function to do the conversions. ;)
Did you try the formulas I suggested in post #4?

Columns D:F below use standard formulas, columns H:J use your custom function. The results are the same.
Perhaps there is something different with your data that means these formulas don't give the results you want?

If for some reason you really want to stick with the custom function and use the SUM function, yet another (non-array) alternative is shown in columns L:N.

Excel Workbook
ABCDEFGHIJKLMN
1Time 1Time 2Time 1Time 2SumTime 1Time 2SumTime 1Time 2Sum
2606701:0001:0702:0701:0001:0702:0701:0001:0702:07
3752001:1500:2001:3501:1500:2001:3501:1500:2001:35
4210000:0201:4001:4200:0201:4001:4200:0201:4001:42
51200120020:0020:0040:0020:0020:0040:0020:0020:0040:00
SUM
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,617
Messages
6,179,914
Members
452,949
Latest member
beartooth91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top