excel - VBA 中的自动筛选

标签 excel vba autofilter

<分区>

我想自动过滤列“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

关于excel - VBA 中的自动筛选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52787967/

相关文章:

excel - 没有指数数字的 CSV 文件创建

ms-access - Access 2010 VBA - 查询开始时打开的对话框,查询完成时关闭的对话框

html - 如何在不使用 Sendkeys 的情况下在 Excel 中运行网站表单?

excel - 如何自动过滤表内容并计算结果行数?

excel - 如何改进此查找(查找最后一个相关项目)?

excel - VBA 选择具有部分名称的工作簿

Excel 崩溃设置工作表可见性

excel - 运行时错误 '9' : subscript out of range when generating an array

arrays - 隐藏重复的单元格而不使用辅助列

vba - 基于多个单元格值的 Excel 宏过滤器