我正在尝试在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
答案 0 :(得分: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
答案 1 :(得分:1)
我今天刚刚发现的一些关于使用VBA代码过滤空白的东西。请务必在需要空白单元格的所有代码中包含此内容:
'获得空白行
WorkRange.AutoFilter Field:=1, Criteria1:="=", Operator:=xlOr, Criteria2:="=" & ""
'用空白隐藏行...与"<>"相同的想法对于运营商
WorkRange.AutoFilter Field:=1, Criteria1:="<>", Operator:=xlOr, Criteria2:="<>" & ""
第一个条件获取真空白单元格和具有隐藏/不可打印字符的单元格,第二个条件获取包含空字符串的单元格。 Excel用户界面处理得很好,但VBA代码需要两个标准。
这个未记载的警告只花了我几个小时的调试,更不用说我的经理关于&#34的一些选择词了;我以为我们正在删除这些栏目中的空白......&#34;
想到我会分享,希望能为你省下一些麻烦。
答案 2 :(得分: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
答案 3 :(得分:0)
我知道这个踏板已经很长了。但只是想分享。要过滤掉空白单元格,可以使用以下标准使用自动过滤器:
Worksheets("sheet name").Range("A1").autoFilter Field:=18, Criteria1:=(Blanks)
&#34;现场&#34;是指列号。对于&#34; Criteria1&#34;,它可以是
Criteria1:=(Blanks)
或
Criteria1:="="
或
Criteria1:=""