Excel公式将包含周、天、小时的字符串转换为小时
我通常从票务工具获取excel报告,用于跟踪用户工作,格式如下:Excel公式将包含周、天、小时的字符串转换为小时,excel,Excel,我通常从票务工具获取excel报告,用于跟踪用户工作,格式如下: 3d 5h 1w 2d 6h 以上字符串以w周、h小时、d天表示。基本上,这里1天代表8小时,1周代表5天。在上述示例中,3d 5h应返回“29” 我正在寻找一个excel公式,将这些值转换为仅小时格式 提前感谢。您最好创建一个UDF来执行此操作 Function GetTotalHours(hourValues As Range) As Long Dim hourPartsArray Dim totalHours As Lon
3d 5h
1w 2d 6h
以上字符串以w周、h小时、d天表示。基本上,这里1天代表8小时,1周代表5天。在上述示例中,3d 5h应返回“29”
我正在寻找一个excel公式,将这些值转换为仅小时格式
提前感谢。您最好创建一个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)
这里有一个方法:
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
希望这能给你一个公式:
="="&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"w","*40+"),"d","*8+"),"h","")," ","")
然后在顶部复制、粘贴特殊值,并将
=
替换为=
我认为前面的答案将为您提供到达实际目的地的部分途径@DDay这个答案在我的情况下不起作用。在我的例子中,1天应该转化为8小时。