【发布时间】:2020-10-10 08:34:09
【问题描述】:
我的代码运行完美,但是当增加持续时间超过 24 小时时,代码返回第二天的时间。请看图片:
例如:
CELL(C3)-0500_1145-DURATION IS 6.45
CELL(D3)-CTC-THE CODE WILL IGNORE AND MOVE TO NEXT CELL
CELL(E3)-0500_1145-DURATION IS 6.45
CELL(F3)-0500_1145-DURATION IS 6.45----TOTAL
DURATION=6.45(C3)+6.45(E3)+6.45(F3)=20.15
CELL(G3) & CELL(I3)-OFF -THE CODE WILL IGNORE AND MOVE TO NEXT CELL
CELL(H3)-1000_1800(ACP)-DURATION IS 8
虽然代码在这里计算持续时间,即 8 小时,但是当系统将所有持续时间相加时,它应该给出 28:15,但系统将其作为第二天并返回总持续时间为 4:15。 我的问题是,当持续时间超过 24 小时时,如何让系统返回 4:15 的 28 小时 15 分钟(28:15)iso。
Sub CalculateHourly()
Dim j As Long
Dim TextTime, wStart, wStop, midnight As String
Dim TrueTime, Temp As Date
Dim Parts As Variant
Dim lRow As Long
Application.Calculation = xlManual
midnight = "24" & ":" & "00"
'Find the last non-blank cell in column A(1)
lRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To lRow
For j = 3 To 9
TextTime = ""
'copy content of the cells
TextTime = ThisWorkbook.Application.Sheets("Sheet1").Cells(i, j).Value
'loop only of cell does not contain any text
If InStr(1, TextTime, "CTC", vbTextCompare) = 0 And InStr(1, TextTime, "OFF", vbTextCompare) = 0 And InStr(1, TextTime, "LEAVE", vbTextCompare) = 0 _
And Not IsEmpty(TextTime) Then
Parts = Split(TextTime, "_")
'Left(Parts(0), 2) of 0430-04
'Right(Parts(0), 2) of 0430-30
wStart = Left(Parts(0), 2) & ":" & Right(Parts(0), 2)
'wStop = Left(Parts(1), 2) & ":" & Right(Parts(1), 2)
wStop = Left(Parts(1), 2) & ":" & Mid(Parts(1), 3, 2)
Debug.Print ("test : " & Format(wStart, "h:mm;@"))
'If timeout is less than timein
If wStart > wStop Then
'Add 24 hours and make the diff
TrueTime = 24 + CDate(CDate(CDate(Format(wStop, "h:mm;@")) - CDate(Format(wStart, "h:mm;@"))))
Else
'if timeout greater than timein
TrueTime = CDate(CDate(CDate(Format(wStop, "h:mm;@")) - CDate(Format(wStart, "h:mm;@"))))
End If
**If (Temp + TrueTime) > 24 Then
TrueTime = 24 + Temp + TrueTime**
Else
TrueTime = Temp + TrueTime
End If
Temp = TrueTime
End If
Next j 'move to the number column in the same row
Cells(i, 10).Value = CDate(Format(Temp, "h:mm;@"))
Temp = Temp - Temp
Next i 'move to the next row
End Sub
【问题讨论】:
-
实际上它是从oracle系统派生的每周轮班,其中班次以timein_timeout格式定义,例如0700_1500,员工每周工作时间应少于30小时,包括他的休息日,持续时间通常计算一个人应该被花名册的小时数。有没有其他方法,因为 DateDiff 没有工作。
-
从您的代码中删除所有
CDate(Format()),并将number format[hh]:mm应用于目标单元格。注意hh周围的括号。