VBA: Double filter on a sheet, how to filter out d

2019-09-14 20:05发布

I need the VBA code to filter out the alarm logs from a certain time window. Alarms appear and clear out on some objects. I need the script the filter out the alarms between two specified times (e.g those which appeared on/after 1AM AND were cleared by/before 3AM), and copy the filtered data to another sheet. Please refer to the attached image.

See the given image, which shows the all the alarm logs. enter image description here

1条回答
闹够了就滚
2楼-- · 2019-09-14 20:24

I had to modify your data somewhat as no raised and cleared time met your criteria.

Option Explicit

Sub betweenTimes()
    With Worksheets("Sheet5")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Range(.Cells(2, "A"), .Cells(.Rows.Count, "G").End(xlUp))
            'as hardcoded values
            '.AutoFilter field:=2, Criteria1:=">=" & Format(Date + TimeSerial(1, 0, 0), "yyyy-mm-dd hh:mm:ss")
            '.AutoFilter field:=3, Criteria1:="<=" & Format(Date + TimeSerial(3, 0, 0), "yyyy-mm-dd hh:mm:ss")
            'as values from I3:J3
            .AutoFilter field:=2, Criteria1:=Format(.Parent.Cells(3, "I").Value2, "\>\=yyyy-mm-dd hh:mm:ss")
            .AutoFilter field:=3, Criteria1:=Format(.Parent.Cells(3, "J").Value2, "\<\=yyyy-mm-dd hh:mm:ss")
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                If CBool(Application.Subtotal(103, .Cells)) Then
                    Union(.Cells, .Cells(1, "XFD")).Copy Destination:=Worksheets("Sheet6").Cells(2, "A")
                End If
            End With
        End With
        If .AutoFilterMode Then .AutoFilterMode = False
    End With
End Sub

enter image description here

查看更多
登录 后发表回答