Converting Time Formats in Excel

后端 未结 2 753
无人及你
无人及你 2020-12-04 03:45

How would I convert time values that are output in as a string (21m49s) into the Excel time value (00:21:49)? If there is no hour value to be given, the cell is not assigned

2条回答
  •  余生分开走
    2020-12-04 04:30

    Possibly as a VBA UDF¹.

    Function realTime(str As String) As Double
        Dim t As Long, vTMs As Variant, vTMP As Variant
    
        vTMs = Array("h", 0, "m", 0, "s", 0)
        vTMP = Array(str & ChrW(8203))
    
        For t = LBound(vTMs) To UBound(vTMs) Step 2
            vTMP = Split(vTMP(0), vTMs(t))
            If IsNumeric(vTMP(0)) Then
                vTMs(t + 1) = Int(vTMP(0))
                vTMP(0) = vTMP(1)
            End If
        Next t
    
        realTime = TimeSerial(vTMs(1), vTMs(3), vTMs(5))
    
    End Function
    

            


    ¹ A User Defined Function (aka UDF) is placed into a standard module code sheet. Tap Alt+F11 and when the VBE opens, immediately use the pull-down menus to Insert ► Module (Alt+I,M). Paste the function code into the new module code sheet titled something like Book1 - Module1 (Code). Tap Alt+Q to return to your worksheet(s).

提交回复
热议问题