+ Reply to Thread
Results 1 to 9 of 9

VBA Convert Time from weird format

  1. #1
    Registered User
    Join Date
    06-26-2013
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    24

    Question VBA Convert Time from weird format

    I am working on a subroutine that will help me transform call records into something useful for analysis.

    Column B contains values that refer to the duration of the phone call, however the formatting is quite strange. Here are some values:

    47s 472ms
    53s 844ms
    55s 164ms
    2mn 17s
    1mn 5s
    1mn 25s

    As you can tell, if the call is less than 1 minute, the format is seconds then milliseconds. If the call is greater than 1 minute, the format is minutes then seconds.

    I need to convert those values to seconds (so ignore the milliseconds when they appear although ideally I would like to round up / down for milliseconds). I would also like the new value to be in seconds so if the time is 1mn 5s, the new value should be 65. If the value is 53s 844ms, the new value should be 54.

    Would you mind helping me write the VBA code? Please let me know if you need any more clarification.

    Thanks!

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: VBA Convert Time from weird format

    Maybe:

    Please Login or Register  to view this content.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    06-26-2013
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: VBA Convert Time from weird format

    Thanks, TMS. I think that is almost right unless I am doing something wrong. When I ran "test" for value 30s 528ms the msgbox returned 12:31:00AM, which is close. It appears the seconds are being assigned the minutes place.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: VBA Convert Time from weird format

    Suggest you post a sample workbook with some typical data. I'm just working with a copy of the text in your post.

    Or, just format the cell as Time


    Regards, TMS

  5. #5
    Registered User
    Join Date
    06-26-2013
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: VBA Convert Time from weird format

    I think I got it using a different approach:

    Please Login or Register  to view this content.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: VBA Convert Time from weird format

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,463

    Re: VBA Convert Time from weird format

    Looked again and, yes, you are/were right ... I missed the 0 hours part out.

    I've changed the code now. Both functions work well but my version rounds the milliseconds as you suggested you would like originally. Not a lot in it

    See the attached workbook.

    Please Login or Register  to view this content.

    Regards, TMS
    Attached Files Attached Files

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: VBA Convert Time from weird format

    Hello bfs3,

    Here is another way using a function that is simpler.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  9. #9
    Registered User
    Join Date
    06-26-2013
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: VBA Convert Time from weird format

    Thanks for the fix, TMShucks. And thank you Leith Ross. I'm learning a lot here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Getting rid of weird time format 01m 16s
    By rooonik in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 06-29-2013, 11:08 AM
  2. Convert date and time in serial number format to text format
    By nda13112 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-11-2013, 02:45 PM
  3. [SOLVED] Time measures in weird format
    By andrewmo in forum Excel General
    Replies: 10
    Last Post: 09-21-2012, 03:41 PM
  4. Excel: How to convert time format into number format that can be added?
    By Frances Jones in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-17-2012, 04:10 PM
  5. [SOLVED] convert time imported as text to time format for calculations
    By batfish in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-27-2005, 07:05 PM

Tags for this Thread

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