【问题标题】:Duration of time more than 24 hours持续时间超过 24 小时
【发布时间】: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 周围的括号。

标签: excel vba


【解决方案1】:

使用这样的函数将格式设置为小时:分钟:

Public Function FormatHourMinute( _
  ByVal datTime As Date, _
  Optional ByVal strSeparator As String = ":") _
  As String

' Returns count of days, hours and minutes of datTime
' converted to hours and minutes as a formatted string
' with an optional choice of time separator.
'
' Example:
'   datTime: #10:03# + #20:01#
'   returns: 30:04
'
' 2005-02-05. Cactus Data ApS, CPH.

  Dim strHour       As String
  Dim strMinute     As String
  Dim strHourMinute As String

  strHour = CStr(Fix(datTime) * 24 + Hour(datTime))
  ' Add leading zero to minute count when needed.
  strMinute = Right("0" & CStr(Minute(datTime)), 2)
  strHourMinute = strHour & strSeparator & strMinute

  FormatHourMinute = strHourMinute

End Function

【讨论】:

  • 超过 24 小时后第二天会出现同样的问题
  • 这是不可能的。请参阅行内注释以获取工作示例,然后调试您的代码。
  • 请给我您的电子邮件地址,将发送您可以检查的 excel 文件
  • 如果您扩展您的问题以包含您当前的代码,这对每个人都会更好。
【解决方案2】:
Sub CalculateHourly2()

    Dim j As Long
    Dim TextTime As String, wStart As Date, wStop As Date, midnight As String
    Dim Parts As Variant
    Dim lRow As Long
    Dim vArray() As Variant, n As Integer
    Application.Calculation = xlManual
    
     'Find the last non-blank cell in column A(1)
    lRow = Cells(Rows.Count, 1).End(xlUp).Row
    For i = 3 To lRow
        n = 0
        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 TextTime <> "" Then '<~~ Unlike the case where the cell is empty, if you put an empty cell into a variable, it is not empty.
                Parts = Split(TextTime, "_")
                'Left(Parts(0), 2) of 0430-04
                'Right(Parts(0), 2) of 0430-30
                wStart = TimeValue(Left(Parts(0), 2) & ":" & Right(Parts(0), 2))
                'wStop = TimeValue(Left(Parts(1), 2) & ":" & Right(Parts(1), 2))
                 wStop = Left(Parts(1), 2) & ":" & Mid(Parts(1), 3, 2) '<~~  Since other characters have been added, the mid sentence must be used.
                n = n + 2
                ReDim Preserve vArray(1 To n)
                vArray(n - 1) = wStart
                vArray(n) = wStop
            End If
            
        Next j 'move to the number column in the same row
    
        'Cells(i, 10).Value = CDate(Format(Temp, "h:mm;@"))
        If n > 0 Then
            Cells(i, 10).Value = getTime(vArray)
            Cells(i, 10).NumberFormat = "[hh]:mm"
        End If
    Next i 'move to the next row

End Sub

Function getTime(Other() As Variant)
    Dim myTime As Date, s As Date, e As Date
    Dim i As Integer

    For i = LBound(Other) To UBound(Other) Step 2
        s = Other(i)
        e = Other(i + 1)
        If s > e Then
            e = e + 1
        End If
        myTime = myTime + e - s
    Next i
    getTime = myTime
End Function

图纸图片

【讨论】:

  • 代码被困在 wStart = TimeValue(Left(Parts(0), 2) & ":" & Right(Parts(0), 2))
  • @VishalSoodursun,我已插入图像。请与您的数据比较后回复。
  • @VishalSoodursun,如果有效,请接受回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 2012-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
  • 2016-11-16
相关资源
最近更新 更多