如何根据用户指定的日期过滤和删除数据行?

时间:2019-06-20 15:58:24

标签: excel vba

我有一些数据要根据开始日期和结束日期进行过滤。

开始日期和结束日期在“主发布者内容”中都有自己的列。 开始日期在G列中,结束日期在H列中。

我正在尝试根据“输入信息”选项卡上用户指定的日期范围来过滤和删除数据行,用户指定的开始日期在单元格C2中,而用户指定结束日期在单元格E2中。

例如,如果单元格C2April 1st, 2019而单元格E2April 30th, 2019,我想删除不在这些日期之间的数据行。

此代码仅适用于结束日期。说没有发现细胞。

Public Sub DeleteRowsWithAutofilterDates()

    Dim wksData As Worksheet
    Dim lngLastRow As Long
    Dim rngData As Range
    Dim StartDate As Range
    Dim EndDate As Range

    'Set references up-front
    Set wksData = ThisWorkbook.Worksheets("Master Publisher Content")
    Set StartDate = ThisWorkbook.Worksheets("Enter Info").Range("C2")
    Set EndDate = ThisWorkbook.Worksheets("Enter Info").Range("E2")

    'Identify the last row and use that info to set up the Range
    With wksData
        lngLastRow = .Range("G" & .Rows.Count).End(xlUp).Row
        Set rngData = .Range("G2:G" & lngLastRow)
    End With

    '------------------------------------------------------------
    Application.DisplayAlerts = False
    With rngData
        'Apply the Autofilter method to the first column of
        .AutoFilter Field:=1, Criteria1:="<=EndDate"

        'Delete the visible rows while keeping the header
        .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Rows.Delete
    End With

    '----------------------------------------------------------------

    Application.DisplayAlerts = True

    'Turn off the AutoFilter
    With wksData
        .AutoFilterMode = False
        If .FilterMode = True Then
            .ShowAllData
        End If
    End With

    'Let the user know the rows have been removed
    MsgBox "Damn son! Rows removed."

End Sub

1 个答案:

答案 0 :(得分:0)

您要查找与开始和结束日期有关的单元格值,而不是范围本身。取Dim StartDate As RangeDim EndDate As Range,使其分别为Dim StartDate As DateDim EndDate As Date

此更改之后Set StartDate = ThisWorkbook.Worksheets("Enter Info").Range("C2")StartDate = ThisWorkbook.Worksheets("Enter Info").cells(2, "C").value 还有Set EndDate = ThisWorkbook.Worksheets("Enter Info").Range("E2")EndDate = ThisWorkbook.Worksheets("Enter Info").cells(2, "E").value

这将为您获取单元格的值,而不是其范围。

lngLastRow = .Range("G" & .Rows.Count).End(xlUp).Row应该为lngLastRow = .Cells(.Rows.Count, "G").End(xlUp).row

我不确定您要遍历多少行,但是如果有很多行,实际上删除可能会很慢,因此这将清除该行然后进行排序。

    Dim wksData As Worksheet
    Dim lngLastRow As Long
    Dim LastCol As Integer
    Dim StartDate As Date
    Dim EndDate As Date

    Dim tDate As Date
    Dim iter As Long

    Set wksData = ThisWorkbook.Worksheets("Master Publisher Content")

    StartDate = ThisWorkbook.Worksheets("Enter Info").Cells(2, "C").value
    EndDate = ThisWorkbook.Worksheets("Enter Info").Cells(2, "E").value

    With wksData
        lngLastRow = .Cells(.Rows.Count, "G").End(xlUp).row

        For iter = 2 To lngLastRow
            if .Cells(iter, "G").value = "" or isnull(.Cells(iter, "G").value) then
                .Rows(iter) = ""
            else
                tDate = .Cells(iter, "G").value

                If tDate < StartDate Or tDate > EndDate Then
                    .Rows(iter) = ""
                End If
            end if
        Next
        LastCol = .Cells.Find(What:="*", after:=ActiveSheet.Cells(1, 1), LookIn:=xlFormulas, LookAt:= _
            xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, MatchCase:=False).Column
        .Range(Cells(1, 1), Cells(lngLastRow, LastCol)).Sort key1:=.Columns(1), Order1:=xlAscending, Header:=xlYes
    End With