以1h 30m和x2B格式表示的总时间;excel中的2h 40m

以1h 30m和x2B格式表示的总时间;excel中的2h 40m,excel,excel-formula,formula,formulas,Excel,Excel Formula,Formula,Formulas,我从一张桌子上得到一些花时间的格式。我想将它们添加到excel表中,并计算它们的总和 时间格式为 1h 30m 及 我需要的总数是 4h 15m 默认情况下,是否有用于此的公式?如果没有,有人能帮我吗?使用帮助列和自定义格式: 您可以使用用户定义的函数轻松完成此操作。下图在顶部两个单元格中显示了一个与您相近的示例,在底部单元格中显示了公式=sumhm(a1:a2): 执行此操作的代码相对简单: Option Explicit Function sumhm(rng As Range)

我从一张桌子上得到一些花时间的格式。我想将它们添加到excel表中,并计算它们的总和

时间格式为

1h 30m

我需要的总数是

4h 15m

默认情况下,是否有用于此的公式?如果没有,有人能帮我吗?

使用帮助列和自定义格式:


您可以使用用户定义的函数轻松完成此操作。下图在顶部两个单元格中显示了一个与您相近的示例,在底部单元格中显示了公式
=sumhm(a1:a2)

执行此操作的代码相对简单:

Option Explicit

Function sumhm(rng As Range)
    Dim pos As Integer
    Dim cell As Range
    Dim str As String
    Dim minutes As Long

    ' Process each cell in the range '

    minutes = 0
    For Each cell In rng
        ' Get string from cell, work out minutes, add to tally '

        str = cell.Text
        pos = InStr(1, str, "h")
        If pos > 0 Then
            minutes = minutes + 60 * CInt(Left(str, pos - 1))
            str = Mid(str, pos + 1)
        End If

        pos = InStr(1, str, "m")
        If pos > 0 Then
            minutes = minutes + CInt(Left(str, pos - 1))
        End If
    Next

    ' All done, revert to XhYm format '

    sumhm = CStr(Int(minutes / 60)) & "h" & CStr(minutes Mod 60) & "m"
End Function
使用UDF的好处是,您可以获得任意复杂的范围(如
C5:D9、G9:H16、B14:D18
),并且它可以正常工作

它还集中了它,所以有一个地方,你可以修复一些事情,比如用户在字符串中加入空格,让你的生活陷入地狱。或者您可能在我的实现中发现的任何错误(或者您可能需要的增强功能,例如处理类似于
3y7w2d9h33m
)请尝试以下操作:

=TIMEVALUE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," ",":"),"h",""),"m",""))
需要将时间解析为原始数字。。。这可以通过left()&mid()完成,但我认为嵌套的Substitute()调用使它更具可读性

由内而外,
1.将“”改为冒号,然后将结果传递到下一级
2.将“h”更改为“”(即删除“h”),将结果传递到下一级
3.将“m”更改为“”(即删除“m”),将结果传递到下一级
4.结果是需要转换为时间值的文本值

您需要将时间单元格的格式设置为hh:mm

总数是上述单元格的总和()

祝你好运

=TIMEVALUE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1," ",":"),"h",""),"m",""))