+ Reply to Thread
Results 1 to 3 of 3

Need UDF to return time format from string input

Hybrid View

  1. #1
    Registered User
    Join Date
    11-11-2011
    Location
    Maine, USA
    MS-Off Ver
    Excel 2010, Windows 7 or XP
    Posts
    16

    Need UDF to return time format from string input

    I have a lot of login logout data that that displays time as, for example, "1:37:22PM" as a string. I'm trying to make a UDF that inputs time in that format (I've attached a workbook with a small sample dump of the times) and outputs the correct time format so that I can use comparison operators. Unfortunately, I seem to be incredibly bad at parsing this and keep messing up. Any tips or help would be incredibly appreciated,

    Thanks!

    testbook.xlsx

    Edit: Apparently, I also am bad at attachements.
    Last edited by Andrew_Harris; 10-25-2012 at 10:08 PM.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,655

    Re: Need UDF to return time format from string input

    try this
    Sub test()
        Dim r As Range, temp As Date
        Application.ScreenUpdating = False
        On Error Resume Next
        With Range("a1").CurrentRegion
            For Each r In .Columns("a:b").Cells
                Err.Clear
                temp = CDate(r.Value)
                If Err = 0 Then
                    r.NumberFormat = "h:mm:ss AM/PM"
                    r.Value = temp
                End If
            Next
        End With
        Application.ScreenUpdating = True
    End Sub

  3. #3
    Registered User
    Join Date
    11-11-2011
    Location
    Maine, USA
    MS-Off Ver
    Excel 2010, Windows 7 or XP
    Posts
    16

    Re: Need UDF to return time format from string input

    Thank you so much! I had no idea the CDate() function existed. Remarkably helpful.

+ 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