+ Reply to Thread
Results 1 to 9 of 9

Converting duration or time into a number

Hybrid View

  1. #1
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    24

    Converting duration or time into a number

    Hi, I have a dataset with a column that contains the duration of an event, that lasts up to 24 hours. The format is as follows: xxh xxm where h stands for hours and m stands for minutes. So if an event lasts 20 minutes it'll say 20 m. If an event lasts 2 hours 24 minutes it'll say 2 h 24 m. If it lasts 2 hours it'll just say 2 h. Is there a function I can write, so "20 m would be read as "20" and 2 h 24 m would be read as "144"? I just want the value in minutes. I can't sum anything or use these values in functions until I convert them into numbers. Thanks for your help!

    -A

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Converting duration or time into a number

    Hi

    Not sure if this will work for you...in a new column, use this copied down, and format as number...

    =HOUR(A1)*60+MINUTE(A1)
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Converting duration or time into a number

    I tried it but it didn't work. Maybe I need to format the column with the durations? "12 m" is simply read as "12 m" and not as a number so Excel can't recognize what to format as minutes and hours

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Converting duration or time into a number

    I can't sum anything or use these values in functions until I convert them into numbers. Thanks for your help!
    Your option will give you text

    Is the time you are using an entered time, or the result if a calculation? If you F2 (edit) F9 (calc) what shows in the forumla bar?

  5. #5
    Registered User
    Join Date
    09-26-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Converting duration or time into a number

    I don't understand your question. The time or duration, whatever you wish to call it, is entered in. So I just have a column of 12 m, 2 h, 3 h, etc etc. with no calculations. I want to make calculations using these numbers, but can't because of the letter "m" and "h". I guess one option would be to find and replace all ms with a blank, and then find all h's and replace with a blank, then multiply it by 60.

  6. #6
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Converting duration or time into a number

    =IFERROR(VALUE(LEFT(A1,FIND("h",A1,1)-1)),0)*60+IFERROR(VALUE(MID(A1,IFERROR(FIND("h",A1,1),0)+1,3)),0)

    its clunky but I tested against
    2 h 24 m
    2 h
    20 m
    and result were as expected...but you must be consistant on your input....HTH
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  7. #7
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Converting duration or time into a number

    OK I think I understand better now, I thought you meant that the data (duration) was entered as time (2:24)

    Try this instead (assuming your data is in A1)...
    =LEFT(A1,2)*60+(MID(A1,SEARCH("m",A1,1)-3,2)*1)

    It would, of course, make things much easier if the data was entered correctly inthe 1st place AS time, using the time format 2:24

    Edit: @ judgeh just fyi, you wont need the VALUE 1st part of your formula, you are already *60. and for the 2nd part, just *1, then you dont need the VALUE there either Nice solution though
    Last edited by FDibbins; 03-27-2013 at 05:21 PM.

  8. #8
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Converting duration or time into a number

    thanks....I guess I don't trust Excel to Cast it for me....but you are definitely correct....

  9. #9
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,719

    Re: Converting duration or time into a number

    This solution should also work with the format suggested

    =SUM(MID(0&A1&"000",FIND({"h","m"},A1&"xxxhm")-2,2)*{60,1})
    Audere est facere

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1