VBA可过滤两个列中的值并删除行

时间:2019-11-20 11:10:58

标签: excel vba filter

我正在研究sub()导入每月收到的.csv(这是我的第一个宏)。 该文件的结构非常简单:共有5列,行数可变。

在A列中有日期(dd / mm / yyyy),B代码,C名称,D和E统计信息(数字)。 在第一行中,我创建了一个带有标题的行。 这里的代码:

Sub ImportCSV()

    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;D:\File.csv", Destination:=Range("$A$1"))
        Application.ScreenUpdating = False
        .Name = "FileName"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 850
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = True
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
        .TextFileDecimalSeparator = "."
        .TextFileThousandsSeparator = ","
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
         Application.ScreenUpdating = True
    End With

    Range("A1").EntireRow.Insert
    Cells(1, 1) = "DATE"
    Cells(1, 2) = "CODE"
    Cells(1, 3) = "NAME"
    Cells(1, 4) = "STATS1"
    Cells(1, 5) = "STATS2"
    Rows("1:1").Select
    Selection.Font.Bold = True
    Selection.HorizontalAlignment = xlCenter

    Columns("D").NumberFormat = "#,##0.00"
    Columns("D").HorizontalAlignment = xlCenter
    Columns("E").NumberFormat = "#,##0.00"
    Columns("E").HorizontalAlignment = xlCenter

    Range("A1:E1").Select
    Selection.AutoFilter
    Cells.Select
    Cells.EntireColumn.AutoFit

    Range("C2").Select
    ActiveWindow.FreezePanes = True

End Sub

现在,我想用过滤器清洁这些纸:

  • 可以从A列中选择上个月的最后一天(通常是 我在当月的第一天收到文件。在 A列也可能是当月的日期,因此我无法选择 最后一天...或者,我在考虑一个输入框),然后 然后删除所有其他日期; 在此列中,我有重复的日期
  • 然后,该过滤器位于C列上,应应用3种不同的过滤器:
      C列中的
    1. 单元格带有单词“ x” ,然后删除这些行;
    2. C列中的
    3. 单元格带有单词“ y” ,然后删除这些行;
    4. C列中的
    5. 单元格没有单词“ z” ,然后删除这些行;

希望一切都清楚...

1 个答案:

答案 0 :(得分:1)

您可以在这里合作,并从中获得一些想法。我用一些示例数据做了一个例子。它有点广泛(阅读:它可以变得更紧凑),但我希望您能够理解这种情况。


样本数据:

enter image description here


示例代码:

Option Explicit

Sub Filtering()

Dim lr As Long
Dim rng As Range

With Sheet1 'Change according to your sheets CodeName

    'Retrieve the last used row
    lr = .Cells(.Rows.Count, 1).End(xlUp).Row

    'Set your range object where you want to apply a filter
    Set rng = .Range("A1:E" & lr)

    With rng

        'Apply first two filters in one go
        .AutoFilter 3, Array("*X*", "*Y*"), xlFilterValues

        'Test for visible rows and if so, delete them
        If .SpecialCells(12).Count > 5 Then .Offset(1).Resize(lr - 1, 5).Rows.EntireRow.Delete

        'Apply second filter
        .AutoFilter 3, "<>*Z*", xlFilterValues

        'Test for visible rows and if so, delete them
        If .SpecialCells(12).Count > 5 Then .Offset(1).Resize(lr - 1, 5).Rows.EntireRow.Delete

        'Remove filter
        .AutoFilter

    End With

End With

End Sub

结果:

enter image description here