Excel时间论坛

发布于 2024-11-28 08:09:15 字数 1043 浏览 0 评论 0原文

我试图找出时间表上的进出时间之间的差异来计算工作时间。

我有以下数据:

----A----------B-----------C----------D-------
----Time In----Time Out----Time In----Time Out
1---11:15 AM---12:05 PM----1:05 PM----1:10 PM
2---1:05 PM----1:20 PM-----2:20 PM----4:00 PM
3---11:35 PM---12:05 AM----1:05 AM----1:30 AM
4---1:20 PM----2:20 PM------------------------

这些单元格全部格式化为时间 (HH:MM AM/PM)

我使用以下公式来计算工作时间:

=(SUM(B1-A1)*24)+(SUM(D1-C1)*24)

该公式产生以下结果:

----A----------B-----------C----------D-----------E----------
----Time In----Time Out----Time In----Time Out----TOTAL HOURS
1---11:15 AM---12:05 PM----1:05 PM----1:10 PM-----0.92-------
2---1:05 PM----1:20 PM-----2:20 PM----4:00 PM-----1.92-------
3---11:35 PM---12:05 AM----1:05 AM----1:30 AM-----(23.08)----
4---1:20 PM----2:20 PM----------------------------1.00-------

第 1、2 行和第 2 行第 4 行显示正确,但第 3 行显示错误。它应该是 0.92 而不是 -23.08。我知道发生这种情况是因为公式无法判断 B 列(上午 12:05)是在第二天。

不幸的是,添加日期不是一个选项……只能添加时间。可以编写一个公式来解决这个限制吗?

I'm trying to find the difference between in/out times on a timesheet to calculate hours worked.

I have the following data:

----A----------B-----------C----------D-------
----Time In----Time Out----Time In----Time Out
1---11:15 AM---12:05 PM----1:05 PM----1:10 PM
2---1:05 PM----1:20 PM-----2:20 PM----4:00 PM
3---11:35 PM---12:05 AM----1:05 AM----1:30 AM
4---1:20 PM----2:20 PM------------------------

These cells are all formatted as Time (HH:MM AM/PM)

I'm using the following formula to calculate hours worked:

=(SUM(B1-A1)*24)+(SUM(D1-C1)*24)

This formula produces the following results:

----A----------B-----------C----------D-----------E----------
----Time In----Time Out----Time In----Time Out----TOTAL HOURS
1---11:15 AM---12:05 PM----1:05 PM----1:10 PM-----0.92-------
2---1:05 PM----1:20 PM-----2:20 PM----4:00 PM-----1.92-------
3---11:35 PM---12:05 AM----1:05 AM----1:30 AM-----(23.08)----
4---1:20 PM----2:20 PM----------------------------1.00-------

Rows 1, 2, & 4 are displaying properly, but Row 3 is displayed wrong. It should be .92 NOT -23.08. I know this is happening because the formula can't tell that column B (12:05 AM) is on the following day.

Unforunately, adding the date is not an option... time only. Can a formula be written to work around this limitation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落日海湾 2024-12-05 08:09:15

将结果数模 24,它应该始终为您提供正数。

你的等式变成:

=MOD(SUM((B1-A1)*24),24) + MOD((SUM(D1-C1)*24),24) 

Take the result number modulo 24, it should give you a positive number all the time.

Your equation becomes:

=MOD(SUM((B1-A1)*24),24) + MOD((SUM(D1-C1)*24),24) 
舂唻埖巳落 2024-12-05 08:09:15

如果结果小于零,则加 24。

If the result is less than zero, add 24.

掩耳倾听 2024-12-05 08:09:15

我正在为那些可能想知道的人添加一个 VBA 答案,因为这是一个很好的问题,也是其他人可能遇到的问题。 VBA 更灵活,因为您可以使用不同的时间格式,但它仍然可以工作(例如,您可以在时间中包含实际日期)。这利用了 duffymo 的解决方案。

Function HoursWorked(t1 As Date, t2 As Date, _
                     t3 As Date, t4 As Date) As Double

Application.ScreenUpdating = False
Dim shift1 As Double
Dim shift2 As Double

shift1 = DateDiff("n", t1, t2) / 60
If shift1 < 0 Then
    shift1 = shift1 + 24
End If

shift2 = DateDiff("n", t3, t4) / 60
If shift2 < 0 Then
    shift2 = shift2 + 24
End If

HoursWorked = shift1 + shift2
Application.ScreenUpdating = True

End Function

请注意,如果班次 3 和班次 4 为空,该功能仍将正常运行。另请注意,我使用秒然后除以 60 以确保精度不会丢失(如果在 DateDiff 函数中使用“h”而不是“n”,则会丢失精度)。

如果您只想获取两个日期/时间之间的小时数,您可以使用以下命令:

Function HoursDiff(ByVal date1 As Date, _
                   ByVal date2 As Date) As Double

Application.ScreenUpdating = False
Dim result As Double

result = DateDiff("n", date1, date2) / 60
If result < 0 Then
    result = result + 24
End If

HoursDiff = result
Application.ScreenUpdating = True

End Function

I am adding a VBA answer for those who might want to know as this is a good question and something other people may run into. VBA is more flexible in that you can have differnet formats for the time and it'll still work (for example you can have actual dates included in the time). This utilizes the solution from duffymo.

Function HoursWorked(t1 As Date, t2 As Date, _
                     t3 As Date, t4 As Date) As Double

Application.ScreenUpdating = False
Dim shift1 As Double
Dim shift2 As Double

shift1 = DateDiff("n", t1, t2) / 60
If shift1 < 0 Then
    shift1 = shift1 + 24
End If

shift2 = DateDiff("n", t3, t4) / 60
If shift2 < 0 Then
    shift2 = shift2 + 24
End If

HoursWorked = shift1 + shift2
Application.ScreenUpdating = True

End Function

Note that if shift 3 and 4 are blank the function will still run normally. Also note that I used seconds then divided by 60 to ensure precision is not lost (which it would be if you "h" instead of "n" in the DateDiff function).

And if you just want to get the number of hours between 2 dates/times, you can use this:

Function HoursDiff(ByVal date1 As Date, _
                   ByVal date2 As Date) As Double

Application.ScreenUpdating = False
Dim result As Double

result = DateDiff("n", date1, date2) / 60
If result < 0 Then
    result = result + 24
End If

HoursDiff = result
Application.ScreenUpdating = True

End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文