0

我需要创建一个 Excel 函数,它给出时间(进入时间和退出时间),它计算这两者之间的时间(hh:mm),例如,如果员工在 23:00(晚上 11 点)进入,并在 07 退出:00(早上 7 点),它应该在单元格中写 8:00。我不使用时间减法的原因是因为上一个示例中的情况,因为它会给出错误(早上 7 点低于晚上 11 点)。到目前为止,我编写了以下代码:

Function DIFERENCAHORASMINUTOS(ByVal EntryTime As Date, ByVal ExitTime As Date)
Dim hexit, hentry, mexit, mentry, dminutes, dhours As Integer
hexit = Int(Hour(ExitTime))
hentry = Int(Hour(EntryTime))
mentry = Int(Minute(EntryTime))
mexit = Int(Minute(ExitTime))
If ExitTime < EntryTime Then
    If mexit < mentry Then
        dhours = (hexit + 24) - hentry
        dminutes = (mexit + 60) - mentry
        DIFERENCAHORASMINUTOS = Time(dhours, dminutes, 0)
    Else
        dhours = (hexit + 24) - hentry
        dminutes = mexit - mentry
        DIFERENCAHORASMINUTOS = Time(dhours, dminutes, 0)
    End If
Else
    If mexit < mentry Then
        dhours = hexit - hentry
        dminutes = (mexit + 60) - mentry
        DIFERENCAHORASMINUTOS = Time(dhours, dminutes, 0)
    Else
        dhours = hexit - hentry
        dminutes = mexit - mentry
        DIFERENCAHORASMINUTOS = Time(dhours, dminutes, 0)
    End If
End If

结束功能

但这给了我单元格中的#VALUE 错误。非常感谢您的关注,如果您需要更多信息来帮助我,请提出要求,我会为您提供。

问候

4

2 回答 2

1

用 timeserial() 改变 time()

Function DIFERENCAHORASMINUTOS(ByVal EntryTime As Date, ByVal ExitTime As Date)
Dim hexit, hentry, mexit, mentry, dminutes, dhours As Integer
hexit = Int(Hour(ExitTime))
hentry = Int(Hour(EntryTime))
mentry = Int(Minute(EntryTime))
mexit = Int(Minute(ExitTime))
If ExitTime < EntryTime Then
    If mexit < mentry Then
        dhours = (hexit + 24) - hentry
        dminutes = (mexit + 60) - mentry
        DIFERENCAHORASMINUTOS = TimeSerial(dhours, dminutes, 0)
    Else
        dhours = (hexit + 24) - hentry
        dminutes = mexit - mentry
        DIFERENCAHORASMINUTOS = TimeSerial(dhours, dminutes, 0)
    End If
Else
    If mexit < mentry Then
        dhours = hexit - hentry
        dminutes = (mexit + 60) - mentry
        DIFERENCAHORASMINUTOS = TimeSerial(dhours, dminutes, 0)
    Else
        dhours = hexit - hentry
        dminutes = mexit - mentry
        DIFERENCAHORASMINUTOS = TimeSerial(dhours, dminutes, 0)
    End If
End If
End Function
于 2013-06-07T16:40:34.163 回答
0

要使用公式执行此操作,您可以使用

=MOD(B2-A2,1)

于 2013-06-07T17:09:50.430 回答