Excel time formatting problem

JohnSinde

New Member
Joined
May 3, 2006
Messages
3
I have data in an Excel 2000 worksheet that represents duration values in hours and minutes. Each cell uses the custom format [h]:mm so that fifty six hours and nineteen minutes appears as 56:19. I total a large number of these entries to get values of thousands of hours. Using the same custom format, two thousand three hundred seventeen hours and six minutes appears as 2317:06. I would like to format this total using the comma separator commonly used in decimal numbers to increase readability so that the total appears as 2,317:06.

While this may not be the greatest challenge facing mankind, I have exhausted my limited knowledge of Excel formatting and the virtually unlimited knowledge of Google search results and have yet to come up with a solution. I think it's a mildly interesting problem and I'm sure there are others who would like to know how to do this too. Any suggestions?

John
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
John, one solution is to use a custom function instead of cell formatting. As in:

Code:
Function CustomTimeFormat(rng As Range)
    Dim timeval As Date
    Dim hrs As Long
    Dim min As Integer
    
    On Error GoTo errhand
    timeval = Application.WorksheetFunction.Sum(rng)
    hrs = Int(timeval * 24)
    min = Round(((timeval * 24) - hrs) * 60, 0)
    CustomTimeFormat = Format(hrs, "0,000") & ":" & min
    Exit Function
errhand:
    CustomTimeFormat = "Error!"
End Function


Put this in a standard code module then instead of a sum formula use:

=CustomTimeFormat("A1:A10")

(for instance)
 
Upvote 0
Thanks scifibum. Your code is a bit over my head so I'm looking forward to figuring out exactly why this works. I'll try it out this weekend.


John
 
Upvote 0
This may be ugly, but I believe it "delivers..." Try this formula:
=TEXT(INT(F2)*24,"#,##0")&":"&TEXT(MOD(F2,1),"00")
Where F2 cell contains your un-comma'ed value.
Note: The result will look correct, but will NOT be anything more
than uncalculable text. /s/ Larry
 
Upvote 0
This may be ugly, but I believe it "delivers..." Try this formula:
=TEXT(INT(F2)*24,"#,##0")&":"&TEXT(MOD(F2,1),"00")
Where F2 cell contains your un-comma'ed value.
Note: The result will look correct, but will NOT be anything more
than uncalculable text. /s/ Larry
OOPs... not quite right on minutes...
 
Upvote 0
OK --- Working from scifibum's original code, I came up with the following:

Function TimeFormatWithComma(DataPoint)

Dim Hours As Integer
Dim Minutes As Integer

On Error GoTo ErrorProcess

Hours = Int(DataPoint * 24)
Minutes = ((DataPoint * 24) - Hours) * 60
TimeFormatWithComma = Format(Hours, "#,##0") & ":" & Format(Minutes, "00")

Exit Function

ErrorProcess:
TimeFormatWithComma = "Error!"

End Function

Since I am using DataPoint in other calculations, I decided to do the summation outside of the function so that it could be used elsewhere. I just take the unformatted summation result as a data point and pass it through the formatting function which renders it more readable, but no longer usable as an argument in other formulae. For further calculations, I just go back to the original data point.

I set the formatting to hide leading zeros in the hours value, but always show both positions in the minutes value as suggested by indiantrix.

I removed the rounding function as it seems unnecessary. To my surprise, Excel apparently rounds seconds up and down automatically. On my system, 2800:00:29 (formatted with [h]:mm:ss) comes out of the TimeFormatWithComma function as 2,800:00. 2800:00:30 comes out as 2,800:00. 2800:00:31 comes out as 2,800:01.

I've tested this on my data and it works just fine. Thanks to everyone for all your help.


John
 
Upvote 0
Woo Hooooooo!

I've been grappling with exactly this problem for years. I'd spend a few hours at a time, and always give up.

I'd have preferred that MS have a built-in (or custom) format for it so that I could still use the data in other calculations, but this is the next best thing.

Many thanks to Scifibum and JohnSinde.

Betelgeuse.
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,665
Members
449,462
Latest member
Chislobog

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