使用excel vb创建持续时间

时间:2016-06-02 10:16:19

标签: excel excel-vba vba

我有一些驱动程序数据日志数据,包括时间和超时(24小时格式),例如:

Name        Time in     Time Out

Frank        5:30       7:55

             11:35      16:35

             17:24      19:20

关于这些数据我有点麻烦处理成这样的事情

result

因为我有数百个像这样的数据,如何使用excel vb将这样的数据处理成结果?谢谢

1 个答案:

答案 0 :(得分:0)

正如评论中所提到的,您建议的逐小时划分似乎不会给图表的结果带来准确性或信任。我建议最多10分钟的段,开始和停止时间四舍五入到这些分区。

午夜至凌晨01:00被视为零时,晚上11:00至午夜被视为第二十三小时。虽然时间可以转换,但这似乎不是有效的编码。教育您的用户改为使用正确的时间括号。

Option Explicit

Sub create_time_duration()
    Dim h As Long, trw As Long, drw As Variant
    With Worksheets("Sheet6")
        .Range("E:EU").EntireColumn.Delete
        For h = 0 To 23
            With .Cells(1, 6 + h * 6).Resize(1, 6)
                .EntireColumn.ColumnWidth = 0.6
                .Merge
                With .Resize(1, 1)
                    .NumberFormat = "hh:mm"
                    .Value = TimeSerial(h, 0, 0)
                    .HorizontalAlignment = xlCenter
                    .VerticalAlignment = xlCenter
                    .Font.Size = 10
                End With
            End With
        Next h

        For trw = 2 To .Cells(Rows.Count, "C").End(xlUp).Row
            If Not IsEmpty(.Cells(trw, "A")) Then
                drw = Application.Match(.Cells(trw, "A").Value2, .Columns("E"), 0)
                If IsError(drw) Then
                    .Cells(Rows.Count, "E").End(xlUp).Offset(1, 0) = .Cells(trw, "A").Value2
                    drw = Application.Match(.Cells(trw, "A").Value2, .Columns("E"), 0)
                End If
                .Cells(drw, "F").Resize(1, 144).NumberFormat = ";;;"
            End If
            .Cells(drw, (Hour(.Cells(trw, "B").Value) + 1) * 6 + _
                        CLng(Minute(.Cells(trw, "B").Value) / 10)). _
                        Resize(1, Hour(.Cells(trw, "C").Value - .Cells(trw, "B").Value) * 6 + _
                                  CLng(Minute(.Cells(trw, "C").Value - .Cells(trw, "B").Value) / 10)) = 1
        Next trw

        With .Range(.Cells(2, "E"), .Cells(Rows.Count, "E").End(xlUp))
            With .Resize(.Rows.Count, 144).Offset(0, 1)
                .FormatConditions.Delete
                .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(F2)"
                With .FormatConditions(.FormatConditions.Count)
                    With .Interior
                        .Color = vbGreen
                    End With
                    .StopIfTrue = False
                End With
            End With
        End With
    End With

End Sub

我会留给你创建任何你希望装扮持续时间图的单元格边框。

time_duration_before
在create_time_duration子程序之前

time_duration_after
在create_time_duration子程序之后