excel text to time

后端 未结 2 924
隐瞒了意图╮
隐瞒了意图╮ 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: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)

提交回复
热议问题