Excel公式将包含周,天,小时的字符串转换为小时

时间:2018-05-31 16:54:20

标签: excel

我通常会通过以下格式从售票工具中获取excel报告来跟踪用户的工作量:

3d 5h
1w 2d 6h

上述字符串以w周,h小时,d天表示。基本上,这里1天代表8小时,1周代表5天。在上面的例子中,3d 5h应该返回“29”。

我正在寻找一个excel公式,将这些值转换为仅限小时格式。

提前致谢。

3 个答案:

答案 0 :(得分:0)

最好创建一个UDF来执行此操作。

Function GetTotalHours(hourValues As Range) As Long
Dim hourPartsArray
Dim totalHours As Long
Dim i As Integer
    hourPartsArray = Split(hourValues.Value2, " ")
    For i = 0 To UBound(hourPartsArray)
    Dim tempValue
        If InStr(1, hourPartsArray(i), "w") > 0 Then
            tempValue = Replace(hourPartsArray(i), "w", "")
            totalHours = totalHours + tempValue * 5 * 8
        End If
        If InStr(1, hourPartsArray(i), "d") > 0 Then
            tempValue = Replace(hourPartsArray(i), "d", "")
            totalHours = totalHours + tempValue * 8

        End If
        If InStr(1, hourPartsArray(i), "h") > 0 Then
            tempValue = Replace(hourPartsArray(i), "h", "")
            totalHours = totalHours + tempValue

        End If
    Next i
    GetTotalHours = totalHours
End Function

并使用

=GetTotalHours(A1)

答案 1 :(得分:0)

以这种方式:

Public Function GetTotalHours(r As Range) As Double
    Dim s As String
    s = r.Value2
    s = Replace(s, "w", "*40")
    s = Replace(s, "d", "*8")
    s = Replace(s, "h", "*1")
    s = Replace(s, " ", "+")
    GetTotalHours = Evaluate(s)
End Function

希望有所帮助

答案 2 :(得分:0)

使用公式:

="="&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"w","*40+"),"d","*8+"),"h","")," ","")

然后复制,粘贴特殊值,顶部的值,并将=替换为=