【问题标题】:VBA to filter value in two colums and to delete rowsVBA过滤两列中的值并删除行
【发布时间】:2020-03-16 02:09:05
【问题描述】:

我正在使用 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 个不同的过滤器:
    1. C 列中的单元格带有单词“x”,然后删除这些行;
    2. C 列中的单元格带有单词“y”,然后删除这些行;
    3. C 列中的单元格没有单词“z”,然后删除这些行;

希望一切都清楚...

【问题讨论】:

  • 我们在 C 列中讨论的是什么类型的数据。只是单个单词?
  • 这是一个类似“Name XWord Word”或“Word Y Word”的字符串

标签: excel vba filter


【解决方案1】:

您可以使用以下方法并希望从中获得一些想法。我用一些示例数据做了一个例子。它有点广泛(阅读:它可以变得更紧凑),但我希望你能够理解这是怎么回事。


样本数据:


示例代码:

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

结果:

【讨论】:

  • 谢谢@JvdV!我已经理解了第一部分的逻辑。我不明白 If .SpecialCells(12).Count &gt; 5 Then .Offset(1).Resize(lr - 1, 5).Rows.EntireRow.Delete 并且我没有看到日期的第一个过滤器(例如 31/10/2019)
  • 一个简短的细分:.SpecialCells(12).Count 将首先计算所有可见单元格,以确保在您获得零点击的情况下我们不会出错。然后.Offset(1).Resize(lr-1,5) 将在您的标题行下方获得一个新的范围对象。 5 之所以存在,是因为您谈论的是 5 列范围。
猜你喜欢
  • 1970-01-01
  • 2023-02-02
  • 2016-12-09
  • 2017-06-09
  • 2018-10-27
  • 2013-06-16
  • 1970-01-01
  • 1970-01-01
  • 2021-08-09
相关资源
最近更新 更多