我想自动过滤列“FP” 大约有 200 个名字,我想查看除了选定的几个以外的所有名字。即我想在过滤器下拉框中选择除指定名称之外的所有内容。
Sub Button1_Click()
With Worksheets("RawData")
.AutoFilterMode = False
.AutoFilter Field:=172, Criteria1:="<>John", Criteria2:="<>Kelly",
Criteria4:="<>Don", Criteria5:="<>Chris"
End With
MsgBox (a)
End Sub
您不能选择超过 2 个值来过滤掉。因此,我们构建了一个数组,其中包含您要过滤掉的值,然后我们为这些值着色。然后我们可以使用条件格式来过滤所有没有颜色的值。
缺点是最后一部分删除了应用过滤器的范围的所有条件格式。您可以删除该部分并手动删除包含数组中值的条件格式。
应用于您的数据的 VBA 代码:
Sub Button1_Click()
Dim fc As FormatCondition
Dim ary1 As Variant
fcOrig = ActiveSheet.Cells.FormatConditions.Count
ary1 = Array("John", "Kelly", "Don", "Chris")
For Each str1 In ary1
Set fc = ActiveSheet.Range("FP:FP").FormatConditions.Add(Type:=xlTextString, String:=str1, TextOperator:=xlContains)
fc.Interior.Color = 16841689
fc.StopIfTrue = False
Next str1
ActiveSheet.Range("FP:FP").AutoFilter Field:=1, Operator:=xlFilterNoFill
'Last Part of code remove all the conditional formatting for the range set earlier.
Set fc = Nothing
If fcOrig = 0 Then
fcOrig = 1
For fcCount = ActiveSheet.Cells.FormatConditions.Count To fcOrig Step -1
ActiveSheet.Cells.FormatConditions(fcCount).Delete
Next fcCount
Else
For fcCount = ActiveSheet.Cells.FormatConditions.Count To fcOrig Step -1
ActiveSheet.Cells.FormatConditions(fcCount).Delete
Next fcCount
End If
MsgBox (a)
End Sub