【问题标题】:Excel Function Difference Between Two Dates两个日期之间的Excel函数差异
【发布时间】:2013-06-04 01:01:15
【问题描述】:

我需要创建一个 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 错误。 非常感谢您的关注,如果您需要更多信息来帮助我,请提出请求,我会为您提供。

问候

【问题讨论】:

    标签: excel function vba date


    【解决方案1】:

    要做到这一点,你可以使用一个公式

    =MOD(B2-A2,1)

    【讨论】:

    • 也感谢您简化了我的问题。
    【解决方案2】:

    用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
    

    【讨论】:

      猜你喜欢
      • 2011-10-29
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 2015-09-14
      • 2015-06-21
      • 2020-02-06
      相关资源
      最近更新 更多