excel text to time

后端 未结 2 886
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 08:04

I have an excel table with the time formatted with text like the example below. I need to convert the time from a text string into a usable format. I would be happy if I cou

相关标签:
2条回答
  • 2021-01-14 08:20

    Another UGLY way to achieve your strings into a usable format like dd:hh:mm:ss. Ofcourse if you need only seconds, we could also improvice that ;)

    Please comment once you have tried out. I would call this sort of a situation as a multi-delimiter split combine :) where we treat d, h, m, s as delimiters. There can also be solutions using split function and regexp

    dd:hh:mm:ss

    Code:

    Function multiSplitCombine(ByVal strTime As String) As String
    Dim delimsArray(0 To 3) As Variant
    Dim i As Long, j As Long, k As Long
    
        'set delimiters
        delimsArray(0) = "d"
        delimsArray(1) = "h"
        delimsArray(2) = "m"
        delimsArray(3) = "s"
    
        If Len(strTime) = 0 Then
            multiSplitCombine = "00:00:00:00"
            Exit Function
        End If
    
        For i = LBound(delimsArray) To UBound(delimsArray)
            '-- get the position of the delimiter
            j = InStr(1, strTime, delimsArray(i))
    
            '-- if the delimiter is not found
            If j = 0 Then
                '-- insert 00: into the position after earlier previous delimiter replacement
                strTime = Left(strTime, k) & "00:" & Right(strTime, Len(strTime) - k)
            Else
                k = j
               '-- replace delimiter with semicolon
                strTime = Replace(strTime, delimsArray(i), ":")
            End If
        Next i
    
        '-- strip that last extra semi colon
        strTime = Trim(Left(strTime, Len(strTime) - 1))
    
        '-- remove internal white spaces
        strTime = Replace(strTime, " ", "")
    
        '-- back to sheet
        multiSplitCombine = Application.WorksheetFunction.Text(strTime, "dd:hh:mm:ss")
    End Function
    

    in seconds

    Here what you need to change in above code,

    • Change function name to splitToSeconds
    • replace everything after strTime = Replace(strTime," ", "") by adding following code

      '-- dd:hh:mm:ss format
      strTime = Application.WorksheetFunction.Text(strTime, "dd:hh:mm:ss")    
      
      '-- split by semicolon
      s = Split(strTime, ":")
      
      '-- it has to be 4 elements since the format we insert is d:h:m:s
      splitToSeconds = CDbl(s(0)) * 24 * 60 * 60 + _ 
                       CDbl(s(1)) * 60 * 60 + CDbl(s(2)) * 60 + CDbl(s(3))
      

    Output:

    enter image description here

    0 讨论(0)
  • 2021-01-14 08:29

    This isn't elegant, but it would work as a UDF as long as your format is as described:

    Public Function timeStringToSeconds(strIn As String) As Long
        Dim values
        values = Split(strIn, " ")
        For Each v In values
            Select Case Right$(v, 1)
                Case "d"
                    timeStringToSeconds = timeStringToSeconds + CLng(Left$(v, Len(v) - 1)) * 86400
                Case "h"
                    timeStringToSeconds = timeStringToSeconds + CLng(Left$(v, Len(v) - 1)) * 3600
                Case "m"
                    timeStringToSeconds = timeStringToSeconds + CLng(Left$(v, Len(v) - 1)) * 60
                Case "s"
                    timeStringToSeconds = timeStringToSeconds + CLng(Left$(v, Len(v) - 1))
            End Select
        Next
    End Function
    

    You could use it simply by doing this: in C1 for example: timeStringToSeconds(B1) Or run it on a range by doing something like this: range.value = timeStringToSeconds(range.value)

    0 讨论(0)
提交回复
热议问题