Excel时间论坛
我试图找出时间表上的进出时间之间的差异来计算工作时间。
我有以下数据:
----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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将结果数模 24,它应该始终为您提供正数。
你的等式变成:
Take the result number modulo 24, it should give you a positive number all the time.
Your equation becomes:
如果结果小于零,则加 24。
If the result is less than zero, add 24.
我正在为那些可能想知道的人添加一个 VBA 答案,因为这是一个很好的问题,也是其他人可能遇到的问题。 VBA 更灵活,因为您可以使用不同的时间格式,但它仍然可以工作(例如,您可以在时间中包含实际日期)。这利用了 duffymo 的解决方案。
请注意,如果班次 3 和班次 4 为空,该功能仍将正常运行。另请注意,我使用秒然后除以 60 以确保精度不会丢失(如果在 DateDiff 函数中使用“h”而不是“n”,则会丢失精度)。
如果您只想获取两个日期/时间之间的小时数,您可以使用以下命令:
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.
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: