如果单元格(日期)值在时间范围内,则删除整行

时间:2017-11-15 14:40:14

标签: excel vba

我的编码需要帮助。所以基本上下面的代码将从另一张纸上获取数据并自动关闭。我想输出的只是当天14:00到22:00之间的行。因此,如果SLA目标日期超出该范围,我想删除该行并向上移动,以便输出中间没有空格。我不知道怎么做。

下面是我的代码:

Option Explicit

Private Sub Workbook_Open()
    Call ClearData
    Call ReadDataFromCloseFile
End Sub


Sub ReadDataFromCloseFile()
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False

    Dim src_im As Workbook
    Dim src_fr As Workbook
    Dim src_chm As Workbook
    ' OPEN THE SOURCE EXCEL WORKBOOK IN "READ ONLY MODE".
    Set src_im = Workbooks.Open(ThisWorkbook.Path & "\incident_sla.csv", True, True)
    ' GET THE TOTAL ROWS FROM THE SOURCE WORKBOOK.
    Dim iTotalRows_im As Integer
    iTotalRows_im = src_im.Worksheets("incident_sla").UsedRange.Rows.Count
    Set src_fr = Workbooks.Open(ThisWorkbook.Path & "\sc_req_item_sla.csv", True, True)
    Dim iTotalRows_fr As Integer
    iTotalRows_fr = src_fr.Worksheets("sc_req_item_sla").UsedRange.Rows.Count
    Set src_chm = Workbooks.Open(ThisWorkbook.Path & "\change_task.csv", True, True)
    Dim iTotalRows_chm As Integer
    iTotalRows_chm = src_chm.Worksheets("change_task").UsedRange.Rows.Count

    ' COPY DATA FROM SOURCE (CLOSE WORKGROUP) TO THE DESTINATION WORKBOOK.
    Dim iCnt_im As Integer         ' COUNTER.
    For iCnt_im = 2 To iTotalRows_im
    If Format(src_im.Worksheets("incident_sla").Range("G" & iCnt_im).Value, "mm/dd/yyyy hh:mm") < Format(Date + TimeValue("22:00:00"), "mm/dd/yyyy hh:mm") Then
        Worksheets("Pre-shift").Range("A" & iCnt_im + 2).Formula = src_im.Worksheets("incident_sla").Range("A" & iCnt_im).Formula
        Worksheets("Pre-shift").Range("B" & iCnt_im + 2).Formula = src_im.Worksheets("incident_sla").Range("B" & iCnt_im).Formula
        Worksheets("Pre-shift").Range("B" & iCnt_im + 2).WrapText = False
        Worksheets("Pre-shift").Range("C" & iCnt_im + 2).Formula = src_im.Worksheets("incident_sla").Range("C" & iCnt_im).Formula
        Worksheets("Pre-shift").Range("C" & iCnt_im + 2).Borders.LineStyle = xlContinuous
        Worksheets("Pre-shift").Range("C" & iCnt_im + 2).Borders.Color = RGB(0, 0, 0)
        Worksheets("Pre-shift").Range("D" & iCnt_im + 2).Formula = src_im.Worksheets("incident_sla").Range("D" & iCnt_im).Formula
        Worksheets("Pre-shift").Range("E" & iCnt_im + 2).Formula = src_im.Worksheets("incident_sla").Range("E" & iCnt_im).Formula
        Worksheets("Pre-shift").Range("F" & iCnt_im + 2).Formula = src_im.Worksheets("incident_sla").Range("F" & iCnt_im).Formula
        Worksheets("Pre-shift").Range("G" & iCnt_im + 2).Value = Format(src_im.Worksheets("incident_sla").Range("G" & iCnt_im).Value, "mm/dd/yyyy hh:mm")
        Worksheets("Pre-shift").Range("H" & iCnt_im + 2).Formula = src_im.Worksheets("incident_sla").Range("H" & iCnt_im).Formula
        Worksheets("Pre-shift").Range("H" & iCnt_im + 2).Borders.LineStyle = xlContinuous
        Worksheets("Pre-shift").Range("H" & iCnt_im + 2).Borders.Color = RGB(0, 0, 0)
        Worksheets("Pre-shift").Range("I" & iCnt_im + 2).Value = Format(Date + TimeValue("22:00:00"), "mm/dd/yyyy hh:mm")
        Worksheets("Pre-shift").Range("J" & iCnt_im + 2).Formula = Worksheets("Pre-shift").Range("I" & iCnt_im + 2).Value <= Worksheets("Pre-shift").Range("G" & iCnt_im + 2).Value
    Else
            Worksheets("Pre-shift").Rows(iCnt_im + 2).EntireRow.Delete shift:=xlUp
    End If
    Next iCnt_im

    Sheets("Pre-shift").Range("A3") = "Incident ID"
    Sheets("Pre-shift").Range("A3").Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("A3").Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("B3") = "Title"
    Sheets("Pre-shift").Range("B3").Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("B3").Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("C3") = "Assignee Name"
    Sheets("Pre-shift").Range("C3").Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("C3").Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("D3") = "Status"
    Sheets("Pre-shift").Range("D3").Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("D3").Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("E3") = "Service Type"
    Sheets("Pre-shift").Range("E3").Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("E3").Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("F3") = "Priority"
    Sheets("Pre-shift").Range("F3").Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("F3").Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("G3") = "SLA Target Date"
    Sheets("Pre-shift").Range("G3").Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("G3").Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("H3") = iCnt_im
    Sheets("Pre-shift").Range("H3").Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("H3").Font.Color = RGB(255, 255, 255)
'
'    Dim after As Integer
'
'    For after = 2 To iCnt_im
'    If Worksheets("Pre-shift").Range("J" & after + 2).Value = False Then
'    Else
'            Worksheets("Pre-shift").Rows(after + 2).EntireRow.Delete shift:=xlUp
'    End If
'    Next after

'    If iCnt_im = 2 Then
'    Sheets("Pre-shift").Range("A4").Interior.Color = RGB(191, 191, 191)
'    End If
    Dim iCnt_fr As Integer
    For iCnt_fr = 2 To iTotalRows_fr
'        If CDate(src_fr.Worksheets("sc_req_item_sla").Range("E" & iCnt_fr).Formula) > Now() Then
        Worksheets("Pre-shift").Range("A" & iCnt_fr + iCnt_im + 2).Formula = src_fr.Worksheets("sc_req_item_sla").Range("A" & iCnt_fr).Formula
        Worksheets("Pre-shift").Range("B" & iCnt_fr + iCnt_im + 2).Formula = src_fr.Worksheets("sc_req_item_sla").Range("B" & iCnt_fr).Formula
        Worksheets("Pre-shift").Range("B" & iCnt_fr + iCnt_im + 2).WrapText = False
        Worksheets("Pre-shift").Range("C" & iCnt_fr + iCnt_im + 2).Formula = src_fr.Worksheets("sc_req_item_sla").Range("C" & iCnt_fr).Formula
        Worksheets("Pre-shift").Range("C" & iCnt_fr + iCnt_im + 2).Borders.LineStyle = xlContinuous
        Worksheets("Pre-shift").Range("C" & iCnt_fr + iCnt_im + 2).Borders.Color = RGB(0, 0, 0)
        Worksheets("Pre-shift").Range("D" & iCnt_fr + iCnt_im + 2).Formula = src_fr.Worksheets("sc_req_item_sla").Range("D" & iCnt_fr).Formula
        Worksheets("Pre-shift").Range("E" & iCnt_fr + iCnt_im + 2).Formula = src_fr.Worksheets("sc_req_item_sla").Range("E" & iCnt_fr).Formula
        Worksheets("Pre-shift").Range("F" & iCnt_fr + iCnt_im + 2).Formula = src_fr.Worksheets("sc_req_item_sla").Range("F" & iCnt_fr).Formula
        Worksheets("Pre-shift").Range("G" & iCnt_fr + iCnt_im + 2).Formula = src_fr.Worksheets("sc_req_item_sla").Range("G" & iCnt_im).Formula
'        Else: .EntireRow.Delete
'        End If
    Next iCnt_fr
    Sheets("Pre-shift").Range("A" & iCnt_im + 3).Formula = "Fulfillment ID"
    Sheets("Pre-shift").Range("A" & iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("A" & iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("B" & iCnt_im + 3).Formula = "Title"
    Sheets("Pre-shift").Range("C" & iCnt_im + 3).Formula = "Assignee Name"
    Sheets("Pre-shift").Range("D" & iCnt_im + 3).Formula = "Status"
    Sheets("Pre-shift").Range("E" & iCnt_im + 3).Formula = "SLA Target Date"
    Sheets("Pre-shift").Range("F" & iCnt_im + 3).Formula = "Assignment"
    Sheets("Pre-shift").Range("A" & iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("A" & iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("A" & iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("A" & iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("B" & iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("B" & iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("C" & iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("C" & iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("D" & iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("D" & iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("E" & iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("E" & iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("F" & iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("F" & iCnt_im + 3).Font.Color = RGB(255, 255, 255)

    Dim iCnt_chm As Integer
    For iCnt_chm = 2 To iTotalRows_chm
    Worksheets("Pre-shift").Range("A" & iCnt_chm + iCnt_fr + iCnt_im + 2).Formula = src_chm.Worksheets("change_task").Range("A" & iCnt_chm).Formula
    Worksheets("Pre-shift").Range("B" & iCnt_chm + iCnt_fr + iCnt_im + 2).Formula = src_chm.Worksheets("change_task").Range("B" & iCnt_chm).Formula
    Worksheets("Pre-shift").Range("C" & iCnt_chm + iCnt_fr + iCnt_im + 2).Formula = src_chm.Worksheets("change_task").Range("C" & iCnt_chm).Formula
    Worksheets("Pre-shift").Range("C" & iCnt_chm + iCnt_fr + iCnt_im + 2).HorizontalAlignment = xlLeft
    'Worksheets("Pre-shift").Range("C" & iCnt_chm + iCnt_fr + iCnt_im + 2).WrapText = True
    Worksheets("Pre-shift").Range("D" & iCnt_chm + iCnt_fr + iCnt_im + 2).Formula = src_chm.Worksheets("change_task").Range("D" & iCnt_chm).Formula
    Worksheets("Pre-shift").Range("E" & iCnt_chm + iCnt_fr + iCnt_im + 2).Formula = src_chm.Worksheets("change_task").Range("E" & iCnt_chm).Formula
    Worksheets("Pre-shift").Range("F" & iCnt_chm + iCnt_fr + iCnt_im + 2).Formula = src_chm.Worksheets("change_task").Range("F" & iCnt_chm).Formula
    Worksheets("Pre-shift").Range("D" & iCnt_chm + iCnt_fr + iCnt_im + 2).Borders.LineStyle = xlContinuous
    Worksheets("Pre-shift").Range("D" & iCnt_chm + iCnt_fr + iCnt_im + 2).Borders.Color = RGB(0, 0, 0)
    Worksheets("Pre-shift").Range("G" & iCnt_chm + iCnt_fr + iCnt_im + 2).Formula = src_chm.Worksheets("change_task").Range("G" & iCnt_chm).Formula

    Next iCnt_chm
    Sheets("Pre-shift").Range("A" & iCnt_fr + iCnt_im + 3).Formula = "Parent Change"
    Sheets("Pre-shift").Range("B" & iCnt_fr + iCnt_im + 3).Formula = "Task ID"
    Sheets("Pre-shift").Range("C" & iCnt_fr + iCnt_im + 3).Formula = "Title"
    Sheets("Pre-shift").Range("D" & iCnt_fr + iCnt_im + 3).Formula = "Assignee"
    Sheets("Pre-shift").Range("E" & iCnt_fr + iCnt_im + 3).Formula = "Status"
    Sheets("Pre-shift").Range("F" & iCnt_fr + iCnt_im + 3).Formula = "Planned Start"
    Sheets("Pre-shift").Range("G" & iCnt_fr + iCnt_im + 3).Formula = "Planned End"

    Sheets("Pre-shift").Range("A" & iCnt_fr + iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("A" & iCnt_fr + iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("B" & iCnt_fr + iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("B" & iCnt_fr + iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("C" & iCnt_fr + iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("C" & iCnt_fr + iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("D" & iCnt_fr + iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("D" & iCnt_fr + iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("E" & iCnt_fr + iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("E" & iCnt_fr + iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("F" & iCnt_fr + iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("F" & iCnt_fr + iCnt_im + 3).Font.Color = RGB(255, 255, 255)
    Sheets("Pre-shift").Range("G" & iCnt_fr + iCnt_im + 3).Interior.Color = RGB(0, 0, 0)
    Sheets("Pre-shift").Range("G" & iCnt_fr + iCnt_im + 3).Font.Color = RGB(255, 255, 255)

    ' CLOSE THE SOURCE FILE.
    src_im.Close False             ' FALSE - DON'T SAVE THE SOURCE FILE.
    Set src_im = Nothing
    src_fr.Close False             ' FALSE - DON'T SAVE THE SOURCE FILE.
    Set src_fr = Nothing
    src_chm.Close False             ' FALSE - DON'T SAVE THE SOURCE FILE.
    Set src_chm = Nothing

ErrHandler:
    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

这将正确评估您的时间值。难道你不能不将来源(关闭工作组)的数据复制到目的地工作簿吗?

因为它是今天的日期,所以也要检查

If TimeValue(src_im.Worksheets("incident_sla").Range("G" & iCnt_im).Value) > TimeValue("13:59:59") And TimeValue(src_im.Worksheets("incident_sla").Range("G" & iCnt_im).Value) < TimeValue("22:00:01")  and Int(src_im.Worksheets("incident_sla").Range("G" & iCnt_im).Value) = Date Then