2

我正在尝试在 VBA 中针对同一字段中的三个不同条件应用自动过滤器。应用过滤器后,我想找到所有空白的单元格,有人可以建议吗?

    Sub ApplyAutoFiler()
    Dim ws As Worksheet
    Dim I, j, NumberOfErrors As Long
    IsErrors = False

    Set ws = Sheets("Assessments")

    NumberOfErrors = 0
    Dim Z As Range
    Set Z = Cells(4, 3).EntireColumn.Find("*", SearchDirection:=xlPrevious)
    If Not Z Is Nothing Then
        NumberOfRows = Z.Row
    End If

    For I = 4 To NumberOfRows

   With ws
   .AutoFilterMode = False
  .Range("W4:AA4").AutoFilter Field:=1, Criteria1:=Array("A", "B", "C"), Operator:=xlFilterValues
  .Cells.SpecialCells(xlCellTypeBlanks).Interior.Color = 65535
  .AutoFilterMode = False

   End With

   Next I


   End Sub

在此处输入图像描述

我最终将其作为嵌套的 if 语句

If Range("W" & i).Value = "A" Or Range("W" & i).Value = "B" Or Range("W" & i).Value = "C" Then
     If Range("AD" & i).Value = "" Then
        Range("AD" & CStr(i)).Interior.ColorIndex = 3
        NumberOfErrors = NumberOfErrors + 1
     End If
  End If
4

4 回答 4

1

这似乎让我很接近(它还假设您有一个名为“评估”的工作表):

   Sub ApplyAutoFiler()
   Dim ws As Worksheet

   Set ws = Sheets("Assessments")

 With ws
      .AutoFilterMode = False
      .Range("A:AZ").AutoFilter Field:=23, Criteria1:=Array("a", "b", "c"), Operator:=xlFilterValues
      .Cells.SpecialCells(xlCellTypeBlanks).Interior.Color = 65535
      .AutoFilterMode = False
End With
End Sub
于 2013-04-15T12:38:35.180 回答
1

我今天刚刚发现了一些关于使用 VBA 代码过滤空白的内容。请务必将其包含在需要空白单元格的所有代码中:

' Get Rows with blanks
WorkRange.AutoFilter Field:=1, Criteria1:="=", Operator:=xlOr, Criteria2:="=" & ""

' Hides Rows with blanks ... same idea with the "<>" for operator
WorkRange.AutoFilter Field:=1, Criteria1:="<>", Operator:=xlOr, Criteria2:="<>" & ""

第一个标准获得真正的空白单元格和那些具有隐藏/不可打印字符的单元格,第二个标准获得那些包含空字符串的单元格。Excel 用户界面可以很好地处理这个问题,但 VBA 代码需要这两个条件。

这个未记录的警告只花费了我几个小时的调试时间,更不用说我的经理说了几句“我以为我们正在从这些列中删除空白......”

只是想我会分享,希望能为大家省去一些麻烦。

于 2016-03-31T18:57:02.983 回答
1

我知道这个踏板已经很长了。但只是想分享。要过滤掉空白单元格,您可以使用以下条件使用自动过滤器:

Worksheets("sheet name").Range("A1").autoFilter Field:=18, Criteria1:=(Blanks)

“字段”是指列号。至于“Criteria1”,它可以是

Criteria1:=(Blanks)

或者

Criteria1:="="

或者

Criteria1:=""
于 2017-06-05T12:43:45.373 回答
0

为此,您不需要 VBA。您可以为此使用条件格式。看这个例子

在CF规则中,设置这个公式

=AND($AA5="",OR($W5="a",$W5="b",$W5="c"))

截屏

在此处输入图像描述

如果你仍然想要 VBA,那么看看这个

Sub Sample()
    Dim blnkRange As Range, rng As Range, aCell As Range
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Assessments")

    With ws
        '~~> Sample range for testing purpose
        Set rng = .Range("W4:AA11")

        .AutoFilterMode = False

        With rng
            '~~> Filter on "a","b","c"
            .AutoFilter Field:=1, Criteria1:=Array("a", "b", "c"), Operator:=xlFilterValues
            '~~> Then filter on blanks on Col AA
            .AutoFilter Field:=5, Criteria1:="="

            '~~> Using offset. Assuming that Row 4 has headers
            Set blnkRange = .Offset(1, 0).SpecialCells(xlCellTypeVisible).Cells
        End With

        .AutoFilterMode = False
    End With

    '~~> This will give you the blank cells in Col AA
    If Not blnkRange Is Nothing Then
        For Each aCell In blnkRange
            '~~> Color the blank cells red in Col AA
            If aCell.Column = 27 Then aCell.Interior.ColorIndex = 3
        Next
    End If
End Sub
于 2013-04-15T15:03:08.853 回答