+ Reply to Thread
Results 1 to 9 of 9

VBA Convert Time from weird format

Hybrid View

bfs3 VBA Convert Time from weird... 01-09-2014, 03:47 PM
TMS Re: VBA Convert Time from... 01-09-2014, 04:40 PM
bfs3 Re: VBA Convert Time from... 01-09-2014, 04:47 PM
bfs3 Re: VBA Convert Time from... 01-09-2014, 05:01 PM
TMS Re: VBA Convert Time from... 01-09-2014, 05:01 PM
TMS Re: VBA Convert Time from... 01-09-2014, 05:38 PM
TMS Re: VBA Convert Time from... 01-09-2014, 07:10 PM
bfs3 Re: VBA Convert Time from... 01-09-2014, 10:11 PM
Leith Ross Re: VBA Convert Time from... 01-09-2014, 08:40 PM
  1. #1
    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
    48,134

    Re: VBA Convert Time from weird format

    Maybe:

    Option Explicit
    
    Function fCnvTime(ByRef cell As Range)
    
    ' example VBA call: MsgBox fCnvTime(Range("A3"))
    ' example WS call:  =fcnvtime(A3)
    
    Dim vArray
    Dim lMin As Long, lSec As Long, lMS As Long, i As Long
    Dim sTime As String
    
    On Error Resume Next
    
    If Right(cell, 1) = Chr(160) Then cell = Left(cell, Len(cell) - 1)
    vArray = Split(Trim(cell.Value))
    lMin = 0: lSec = 0: lMS = 0
    
    For i = LBound(vArray) To UBound(vArray)
        If LCase(Right(vArray(i), 2)) = LCase("Mn") Then
            lMin = lMin + --Left(vArray(i), Len(vArray(i)) - Len("Mn"))
            GoTo lblNext
        End If
        If LCase(Right(vArray(i), 2)) = LCase("ms") Then
            lMS = lMS + --Left(vArray(i), Len(vArray(i)) - Len("ms"))
            lSec = lSec + Round(lMS / 1000, 0)
            GoTo lblNext
        End If
        If LCase(Right(vArray(i), 1)) = LCase("s") Then
            lSec = lSec + --Left(vArray(i), Len(vArray(i)) - Len("s"))
            GoTo lblNext
        End If
    lblNext:
    Next 'i
    
    sTime = lMin & ":" & lSec
    
    fCnvTime = TimeValue(sTime)
    
    On Error GoTo 0
    
    End Function
    
    Sub test()
    
    MsgBox fCnvTime(Range("A3"))
    
    End Sub


    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


  2. #2
    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.

  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

    I think I got it using a different approach:

    Public Function ConvertTime(ByVal sTime As String) As Date
    
        Dim vaSplit As Variant
        Dim lSeconds As Long
        Dim lMinutes As Long
    
        Const sSEC As String = "s"
        Const sMIN As String = "mn"
    
        vaSplit = Split(sTime, Space(1))
    
        If Right$(vaSplit(0), 1) = "s" Then
            lSeconds = Val(Replace(vaSplit(0), sSEC, vbNullString))
        Else
            lSeconds = Val(Replace(vaSplit(1), sSEC, vbNullString))
            lMinutes = Val(Replace(vaSplit(0), sMIN, vbNullString))
        End If
    
        ConvertTime = TimeSerial(0, lMinutes, lSeconds)
    
    End Function

+ 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