+ Reply to Thread
Results 1 to 7 of 7

Correcting Time Format

Hybrid View

  1. #1
    Registered User
    Join Date
    11-20-2013
    Location
    Arkansas
    MS-Off Ver
    Excel 2010
    Posts
    7

    Question Correcting Time Format

    I’m running a report on the average hold time of telephone calls. The data I extract for the report exports like this:

    3:09
    :00
    :40
    3:48
    2:18
    :12
    :30
    1:10
    :43
    :52
    1:32
    2:07
    :14
    :00

    I need to re-format the cells to show like this:

    3:09
    0:00
    0:40
    3:48
    2:18
    0:12
    0:30
    1:10
    0:43
    0:52
    1:32
    2:07
    0:14
    0:00

    If I only had 15 or 20, I would change them manually. But, this report has about 200 rows. And, I have 7 different reports to run.

    I’m lost when it comes to making macros and using Visual Basic. Although, I can follow instructions and copy/paste. The code mentioned in this thread:

    http://www.excelforum.com/excel-gene...s-to-time.html

    Sub TransformTimp_2()
    Dim mcell As Range
    For Each mcell In Selection
    If Len(mcell) = 6 Then mcell = 0 + "0" & mcell
    mcell.NumberFormat = "[hh]:mm:ss "
    Next mcell
    End Sub
    Doesn’t work with this particular format. It’s changing the data to this:

    03:09:00
    :00
    :40
    03:48:00
    02:18:00
    :12
    :30
    01:10:00
    :43
    :52
    01:32:00
    02:07:00
    :14
    :00

    How can I change the VB code to give me what I want?

    Thank you.
    Last edited by jfezell; 04-09-2014 at 06:02 PM. Reason: spelling

  2. #2
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Correcting Time Format

    Based on the example data you post, try:
    Sub foo()
    Dim c As Range
    For Each c In Selection
        c.NumberFormat = "hh:mm"
        If Left(c.Value, 1) = ":" Then
            c.Value = TimeValue("0" & c.Value)
        End If
    Next c
    End Sub
    Select the range to transform, run the macro...
    let Source = #table({"Question","Thread", "User"},{{"Answered","Mark Solved", "Add Reputation"}}) in Source

    If I give you Power Query (Get & Transform Data) code, and you don't know what to do with it, then CLICK HERE

    Walking the tightrope between genius and eejit...

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Correcting Time Format

    Try this.
    Sub FixTime()
    Dim rng As Range
    Dim Res As Variant
    
        Set rng = Range("A1", Range("A" & Rows.Count).End(xlUp))
        
        With rng
            Res = Evaluate("INDEX(IF(LEFT(" & .Address & ")="":"", TIMEVALUE(""0""&" & .Address & ")," & .Address & "),,1)")
            .Value = Res
            .NumberFormat = "hh:mm"
        End With
        
    End Sub
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    11-20-2013
    Location
    Arkansas
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: Correcting Time Format

    Thanks Olly and Norie. Olly, your code did the trick - somewhat. It changed this:

    1:21
    :50
    :10
    :51
    :57
    2:12
    1:20
    3:52
    1:18
    1:41
    2:03
    1:53
    :55
    2:58
    :44
    :36
    :45

    To this:

    1:21
    00:50
    00:10
    00:51
    00:57
    2:12
    1:20
    3:52
    1:18
    1:41
    2:03
    1:53
    00:55
    2:58
    00:44
    00:36
    00:45

    I need the cells with minutes (ex: 1:21) to have a leading zero as well (ex: 01:21).

    In other words, look like this:

    01:21
    00:50
    00:10
    00:51
    00:57
    02:12
    01:20
    03:52
    01:18
    01:41
    02:03
    01:53
    00:55
    02:58
    00:44
    00:36
    00:45

    How can I make that happen?

  5. #5
    Forum Expert Olly's Avatar
    Join Date
    09-10-2013
    Location
    Darlington, UK
    MS-Off Ver
    Excel 2016, 2019, 365
    Posts
    6,284

    Re: Correcting Time Format

    Okay, guessing your current values are stored as text. That's the problem with posting values in your post, rather than attaching a workbook, it means I have to guess at how your data is formatted...!

    Try:
    Sub foo()
    Dim c As Range
    For Each c In Selection
        c.NumberFormat = "hh:mm"
        If Left(c.Value, 1) = ":" Then
            c.Value = TimeValue("0" & c.Text)
        Else
            c.Value = TimeValue(c.Text)
        End If
    Next c
    End Sub

  6. #6
    Registered User
    Join Date
    11-20-2013
    Location
    Arkansas
    MS-Off Ver
    Excel 2010
    Posts
    7

    Thumbs up [SOLVED] Correcting Time Format

    [SOLVED] Correcting Time Format

    I wasn't sure if it was okay for me to attach a spread sheet. Yes, you are correct. I was pasting into cells preformatted as TEXT. And, your updated VB code worked. I can now sort by time High/Low and the results are correct. I truly appreciate your help - you and Norie. Reputation added for both. Joe

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Correcting Time Format

    jfezell

    Just wondering, did the code I posted not work?

    It worked for me with the sample data you posted.

+ 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. Replies: 7
    Last Post: 05-20-2013, 01:05 PM
  2. need help correcting a macro-run-time erro 6 - overflow
    By JMJ123 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-29-2013, 12:05 PM
  3. [SOLVED] Correcting a Run-time error ‘445’ problem
    By JPDutch in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-22-2013, 03:19 PM
  4. Correcting format error in multiple files
    By Criterium in forum Word Programming / VBA / Macros
    Replies: 0
    Last Post: 02-08-2010, 06:16 AM
  5. Replies: 2
    Last Post: 09-15-2005, 10:05 AM

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