如何在excel中进行时间计算而不指示结束时间是早上但开始时间是晚上的日期?

时间:2018-01-18 07:26:59

标签: excel vba excel-vba

我计划为我的电子表格做的是(结束时间) - (开始时间),然而,问题是员工有时会在午夜结束,这可能会破坏计算(例如凌晨2点 - 晚上10点=负面数)。写下每个单元格的日期非常繁琐,这在我们的案例中是不切实际的。此外,这会导致更高的人为错误,如果忘记输入日期,则会返回错误的值。

进一步描绘我的情况。我有2个工作表:一个表示生产开始时间和生产结束时间,第二个表示设置开始时间和设置结束时间。每一行生产可以有很多行设置。因此,如果日期值错误,则值会有很大差异。 (例如,下午12点可以在数字上翻译为0.5,但是下午1点1分12​​点的数字值可以在40,000左右。如果进行计算,那将是非常错误的)。基本上,生产和设置时间的所有4个时间值必须保持一致才能进行计算。

以下是我要执行的计算的VBA代码。对于那些觉得有用的人。

Total_rows_NHI = Worksheets("Non-hourly Input").Range("A" & Rows.count).End(xlUp).Row
Total_rows_Set = Worksheets("Set-up Input").Range("A" & Rows.count).End(xlUp).Row
ReDim u4(2 To Total_rows_NHI) As Long
ReDim Case1(2 To Total_rows_NHI) As Long
ReDim Case2(2 To Total_rows_NHI) As Long
ReDim Case3(2 To Total_rows_NHI) As Long
ReDim Case4(2 To Total_rows_NHI) As Long

For k4 = 2 To Total_rows_NHI
    For g4 = 2 To Total_rows_Set
        Setup_Start = Worksheets("Set-up Input").Cells(g4, 5)
        Setup_End = Worksheets("Set-up Input").Cells(g4, 6)
        Prod_Start = Worksheets("Non-hourly Input").Cells(k4, 7)
        Prod_End = Worksheets("Non-hourly Input").Cells(k4, 8)
        Start_Quant = Worksheets("Non-hourly Input").Cells(k4, 9)
        End_Quant = Worksheets("Non-hourly Input").Cells(k4, 10)
        If Worksheets("Non-hourly Input").Cells(k4, 1) = Worksheets("Set-up Input").Cells(g4, 1) _
        And Worksheets("Non-hourly Input").Cells(k4, 2) = Worksheets("Set-up Input").Cells(g4, 2) _
        And Worksheets("Non-hourly Input").Cells(k4, 4) = Worksheets("Set-up Input").Cells(g4, 3) _
        And Worksheets("Non-hourly Input").Cells(k4, 5) = Worksheets("Set-up Input").Cells(g4, 4) Then
            u4(k4) = u4(k4) + (Setup_End - Setup_Start) * 24
            If Setup_Start < Prod_Start And Setup_Start < Prod_End And Setup_End > Prod_Start And Setup_End < Prod_End Then
                Case1(k4) = Case1(k4) + (Setup_End - Prod_Start) * 24
            End If
            If Setup_Start > Prod_Start And Setup_Start < Prod_End And Setup_End > Prod_Start And Setup_End > Prod_End Then
                Case2(k4) = Case2(k4) + (Prod_End - Setup_Start) * 24
            End If
            If Setup_Start > Prod_Start And Setup_Start < Prod_End And Setup_End > Prod_Start And Setup_End < Prod_End Then
                Case3(k4) = Case3(k4) + (Setup_End - Setup_Start) * 24
            End If
            If Setup_Start < Prod_Start And Setup_Start < Prod_End And Setup_End > Prod_Start And Setup_End > Prod_End Then
                Case4(k4) = Case4(k4) + (Prod_End - Prod_Start) * 24
            End If
        End If
    Next g4
Next k4

For k4 = 2 To Total_rows_NHI
    Start_Quant = Worksheets("Non-hourly Input").Cells(k4, 9)
    End_Quant = Worksheets("Non-hourly Input").Cells(k4, 10)
    Prod_Start = Worksheets("Non-hourly Input").Cells(k4, 7)
    Prod_End = Worksheets("Non-hourly Input").Cells(k4, 8)
    Worksheets("Non-hourly Input").Cells(k4, 12) = u4(k4)
    Worksheets("Non-hourly Input").Cells(k4, 13) = Case1(k4) + Case2(k4) + Case3(k4) + Case4(k4)
    Worksheets("Non-hourly Input").Cells(k4, 11) = (End_Quant - Start_Quant) / ((Prod_End - Prod_Start) * 24 - (Case1(k4) + Case2(k4) + Case3(k4) + Case4(k4)))
Next k4

1 个答案:

答案 0 :(得分:1)

使用

=MOD(EndTime-StartTime,1)

这会将(错误的)负数转换为正数。