Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 根据2列的xlOR标准自动筛选和显示行?_Arrays_Excel_Conditional Formatting_Autofilter_Vba - Fatal编程技术网

Arrays 根据2列的xlOR标准自动筛选和显示行?

Arrays 根据2列的xlOR标准自动筛选和显示行?,arrays,excel,conditional-formatting,autofilter,vba,Arrays,Excel,Conditional Formatting,Autofilter,Vba,我希望显示列L=“ABC”所在的行,以及列AA“DEF”所在的行。我试过了 Cells.AutoFilter Field:=12, Criteria1:="ABC", Operator:=xlOr, Field:=28, Criteria1:="<>DEF" Cells.AutoFilter字段:=12,准则1:=“ABC”,运算符:=xlOr, 字段:=28,标准1:=“定义” ,但这似乎只显示L列为“ABC”的行。但是,我想显示行的并集,其

我希望显示列L=“ABC”所在的行,以及列AA“DEF”所在的行。我试过了

Cells.AutoFilter Field:=12, Criteria1:="ABC", Operator:=xlOr, 
                 Field:=28, Criteria1:="<>DEF"
Cells.AutoFilter字段:=12,准则1:=“ABC”,运算符:=xlOr,
字段:=28,标准1:=“定义”
,但这似乎只显示L列为“ABC”的行。但是,我想显示行的并集,其中的L列是“ABC”,或者AA列不是“DEF”。我哪里做错了

TL;DR:要获得实现和理解的最快方法,请向下滚动到第二个选项的“助手”列。 当您有时间将新技术与您的特定情况相匹配并最大限度地提高效率时,请回来探索其他方法

如注释中所述,中的xlOr运算符不能跨多个字段工作;仅适用于单个字段。在我看来,你至少有五种选择


选项1:

为标准设置一个区域。在下面,我使用了AC1:AD3。高级筛选器或条件在连续的行上运行


带有标准区域的样本数据

作为代码:

    Dim crit As Range
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        .Range("AC1:AD1") = Array(.Range("L1").Value, .Range("AA1").Value)
        .Range("AC2") = "ABC"
        .Range("AD3") = "<>DEF"
        Set crit = .Range("AC1:AD3")
        With .Cells(1, 1).CurrentRegion
            .AdvancedFilter Action:=xlFilterInPlace, _
                            CriteriaRange:=crit, Unique:=False
        End With
    End With
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            With .Resize(.Rows.Count - 1, 1).Offset(1, .Columns.Count)
                .Formula = "=OR($L2=""ABC"",$AA2<>""DEF"")"
            End With
        End With
        With .Cells(1, 1).CurrentRegion
            .AutoFilter Field:=.Columns.Count, Criteria1:="TRUE"
            'clean up the 'helper' column and refresh the last_cell afterwards
            If False Then
                .Columns(.Columns.Count).EntireColumn.Delete
                .Parent.UsedRange
            End If
        End With
    End With
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                .FormatConditions.Add Type:=xlExpression, _
                                      Formula1:="=OR($L2=""ABC"",$AA2<>""DEF"")"
                .FormatConditions(.FormatConditions.Count).SetFirstPriority
                .FormatConditions(1).Font.Color = vbRed
            End With
            .AutoFilter Field:=1, _
                        Criteria1:=vbRed, Operator:=xlFilterFontColor
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                .FormatConditions(1).Delete
            End With
        End With
    End With
    Dim d As Long, dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            For d = 2 To .Rows.Count
                If LCase(.Cells(d, "L")) = "abc" Or LCase(.Cells(d, "AA")) <> "def" Then _
                    dict.Add Key:=CStr(.Cells(d, "A").Text), Item:=vbNullString
            Next d
            With .Columns(1)
                .AutoFilter Field:=1, Criteria1:=dict.keys, Operator:=xlFilterValues
            End With
        End With
    End With

    dict.RemoveAll: Set dict = Nothing
    Dim d As Long, rng As Range

    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            .EntireRow.Hidden = False
            For d = 2 To .Rows.Count
                If (LCase(.Cells(d, "L").Value2) <> "abc" And _
                    LCase(.Cells(d, "AA").Value2) = "def") Then
                    If rng Is Nothing Then
                        Set rng = .Rows(d)
                    Else
                        Set rng = Union(rng, .Rows(d))
                    End If
                End If
            Next d
            rng.EntireRow.Hidden = True
        End With
    End With
Dim crit As Range
带工作表(“表1”)
如果.AutoFilterMode,则.AutoFilterMode=False
.Range(“AC1:AD1”)=数组(.Range(“L1”).Value、.Range(“AA1”).Value)
.范围(“AC2”)=“ABC”
.范围(“AD3”)=“DEF”
设置临界值=.Range(“AC1:AD3”)
带.Cells(1,1).CurrentRegion
.AdvancedFilter操作:=xlFilterPlace_
CriteriaRange:=crit,Unique:=False
以
以

选项2:在带有工作表公式的“助手”列上

“辅助”列是进行多次测定的简单方法。将一个根据您的多个条件(例如,
=或($L2=“ABC”,$AA2“DEF”)
解析为真/假的简单公式放入未使用的列中,并在该列上进行.AutoFilter


“助手”列中显示工作表公式的示例数据

作为代码:

    Dim crit As Range
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        .Range("AC1:AD1") = Array(.Range("L1").Value, .Range("AA1").Value)
        .Range("AC2") = "ABC"
        .Range("AD3") = "<>DEF"
        Set crit = .Range("AC1:AD3")
        With .Cells(1, 1).CurrentRegion
            .AdvancedFilter Action:=xlFilterInPlace, _
                            CriteriaRange:=crit, Unique:=False
        End With
    End With
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            With .Resize(.Rows.Count - 1, 1).Offset(1, .Columns.Count)
                .Formula = "=OR($L2=""ABC"",$AA2<>""DEF"")"
            End With
        End With
        With .Cells(1, 1).CurrentRegion
            .AutoFilter Field:=.Columns.Count, Criteria1:="TRUE"
            'clean up the 'helper' column and refresh the last_cell afterwards
            If False Then
                .Columns(.Columns.Count).EntireColumn.Delete
                .Parent.UsedRange
            End If
        End With
    End With
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                .FormatConditions.Add Type:=xlExpression, _
                                      Formula1:="=OR($L2=""ABC"",$AA2<>""DEF"")"
                .FormatConditions(.FormatConditions.Count).SetFirstPriority
                .FormatConditions(1).Font.Color = vbRed
            End With
            .AutoFilter Field:=1, _
                        Criteria1:=vbRed, Operator:=xlFilterFontColor
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                .FormatConditions(1).Delete
            End With
        End With
    End With
    Dim d As Long, dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            For d = 2 To .Rows.Count
                If LCase(.Cells(d, "L")) = "abc" Or LCase(.Cells(d, "AA")) <> "def" Then _
                    dict.Add Key:=CStr(.Cells(d, "A").Text), Item:=vbNullString
            Next d
            With .Columns(1)
                .AutoFilter Field:=1, Criteria1:=dict.keys, Operator:=xlFilterValues
            End With
        End With
    End With

    dict.RemoveAll: Set dict = Nothing
    Dim d As Long, rng As Range

    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            .EntireRow.Hidden = False
            For d = 2 To .Rows.Count
                If (LCase(.Cells(d, "L").Value2) <> "abc" And _
                    LCase(.Cells(d, "AA").Value2) = "def") Then
                    If rng Is Nothing Then
                        Set rng = .Rows(d)
                    Else
                        Set rng = Union(rng, .Rows(d))
                    End If
                End If
            Next d
            rng.EntireRow.Hidden = True
        End With
    End With
与工作表(“表1”)
如果.AutoFilterMode,则.AutoFilterMode=False
带.Cells(1,1).CurrentRegion
使用.Resize(.Rows.Count-1,1).Offset(1、.Columns.Count)
.Formula=“=或($L2=”“ABC”“$AA2”“DEF”“)
以
以
带.Cells(1,1).CurrentRegion
.AutoFilter字段:=.Columns.Count,标准1:=“TRUE”
'清理“helper”列,然后刷新最后一个\u单元格
如果是假的话
.Columns(.Columns.Count).EntireColumn.Delete
.Parent.UsedRange
如果结束
以
以
保持公式尽可能简单。请记住删除“helper”列,并在处理完筛选结果后刷新


选项3:按规则执行

可以通过规则和应用与“helper”列解决方案相同的逻辑。AutoFilter可以根据应用于匹配行的颜色进行过滤


显示规则的示例数据

作为代码:

    Dim crit As Range
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        .Range("AC1:AD1") = Array(.Range("L1").Value, .Range("AA1").Value)
        .Range("AC2") = "ABC"
        .Range("AD3") = "<>DEF"
        Set crit = .Range("AC1:AD3")
        With .Cells(1, 1).CurrentRegion
            .AdvancedFilter Action:=xlFilterInPlace, _
                            CriteriaRange:=crit, Unique:=False
        End With
    End With
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            With .Resize(.Rows.Count - 1, 1).Offset(1, .Columns.Count)
                .Formula = "=OR($L2=""ABC"",$AA2<>""DEF"")"
            End With
        End With
        With .Cells(1, 1).CurrentRegion
            .AutoFilter Field:=.Columns.Count, Criteria1:="TRUE"
            'clean up the 'helper' column and refresh the last_cell afterwards
            If False Then
                .Columns(.Columns.Count).EntireColumn.Delete
                .Parent.UsedRange
            End If
        End With
    End With
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                .FormatConditions.Add Type:=xlExpression, _
                                      Formula1:="=OR($L2=""ABC"",$AA2<>""DEF"")"
                .FormatConditions(.FormatConditions.Count).SetFirstPriority
                .FormatConditions(1).Font.Color = vbRed
            End With
            .AutoFilter Field:=1, _
                        Criteria1:=vbRed, Operator:=xlFilterFontColor
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                .FormatConditions(1).Delete
            End With
        End With
    End With
    Dim d As Long, dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            For d = 2 To .Rows.Count
                If LCase(.Cells(d, "L")) = "abc" Or LCase(.Cells(d, "AA")) <> "def" Then _
                    dict.Add Key:=CStr(.Cells(d, "A").Text), Item:=vbNullString
            Next d
            With .Columns(1)
                .AutoFilter Field:=1, Criteria1:=dict.keys, Operator:=xlFilterValues
            End With
        End With
    End With

    dict.RemoveAll: Set dict = Nothing
    Dim d As Long, rng As Range

    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            .EntireRow.Hidden = False
            For d = 2 To .Rows.Count
                If (LCase(.Cells(d, "L").Value2) <> "abc" And _
                    LCase(.Cells(d, "AA").Value2) = "def") Then
                    If rng Is Nothing Then
                        Set rng = .Rows(d)
                    Else
                        Set rng = Union(rng, .Rows(d))
                    End If
                End If
            Next d
            rng.EntireRow.Hidden = True
        End With
    End With
与工作表(“表1”)
如果.AutoFilterMode,则.AutoFilterMode=False
带.Cells(1,1).CurrentRegion
使用.Resize(.Rows.Count-1、.Columns.Count).Offset(1,0)
.FormatConditions.Add类型:=Xexpression_
公式1:=“=或($L2=”“ABC”“$AA2”“DEF”“)
.FormatConditions(.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).Font.Color=vbRed
以
.自动筛选字段:=1_
准则1:=vbRed,运算符:=xlFilterFontColor
使用.Resize(.Rows.Count-1、.Columns.Count).Offset(1,0)
.FormatConditions(1)。删除
以
以
以
这里明显的好处是没有一列公式可以“清理”


选项4:在具有唯一值的列上使用对象

如果您有一个包含唯一值的列,请在行中循环,将您的条件与列L和AA中的值进行比较。如果匹配,则记录a中唯一列中的值,并将键用作条件数组


显示规则的示例数据

作为代码:

    Dim crit As Range
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        .Range("AC1:AD1") = Array(.Range("L1").Value, .Range("AA1").Value)
        .Range("AC2") = "ABC"
        .Range("AD3") = "<>DEF"
        Set crit = .Range("AC1:AD3")
        With .Cells(1, 1).CurrentRegion
            .AdvancedFilter Action:=xlFilterInPlace, _
                            CriteriaRange:=crit, Unique:=False
        End With
    End With
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            With .Resize(.Rows.Count - 1, 1).Offset(1, .Columns.Count)
                .Formula = "=OR($L2=""ABC"",$AA2<>""DEF"")"
            End With
        End With
        With .Cells(1, 1).CurrentRegion
            .AutoFilter Field:=.Columns.Count, Criteria1:="TRUE"
            'clean up the 'helper' column and refresh the last_cell afterwards
            If False Then
                .Columns(.Columns.Count).EntireColumn.Delete
                .Parent.UsedRange
            End If
        End With
    End With
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                .FormatConditions.Add Type:=xlExpression, _
                                      Formula1:="=OR($L2=""ABC"",$AA2<>""DEF"")"
                .FormatConditions(.FormatConditions.Count).SetFirstPriority
                .FormatConditions(1).Font.Color = vbRed
            End With
            .AutoFilter Field:=1, _
                        Criteria1:=vbRed, Operator:=xlFilterFontColor
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                .FormatConditions(1).Delete
            End With
        End With
    End With
    Dim d As Long, dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            For d = 2 To .Rows.Count
                If LCase(.Cells(d, "L")) = "abc" Or LCase(.Cells(d, "AA")) <> "def" Then _
                    dict.Add Key:=CStr(.Cells(d, "A").Text), Item:=vbNullString
            Next d
            With .Columns(1)
                .AutoFilter Field:=1, Criteria1:=dict.keys, Operator:=xlFilterValues
            End With
        End With
    End With

    dict.RemoveAll: Set dict = Nothing
    Dim d As Long, rng As Range

    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            .EntireRow.Hidden = False
            For d = 2 To .Rows.Count
                If (LCase(.Cells(d, "L").Value2) <> "abc" And _
                    LCase(.Cells(d, "AA").Value2) = "def") Then
                    If rng Is Nothing Then
                        Set rng = .Rows(d)
                    Else
                        Set rng = Union(rng, .Rows(d))
                    End If
                End If
            Next d
            rng.EntireRow.Hidden = True
        End With
    End With
Dim d为长,dict为对象
Set dict=CreateObject(“Scripting.Dictionary”)
带工作表(“表1”)
如果.AutoFilterMode,则.AutoFilterMode=False
带.Cells(1,1).CurrentRegion
对于d=2到.Rows.Count
如果LCase(.Cells(d,“L”)=“abc”或LCase(.Cells(d,“AA”)))“def”,则_
dict.Add Key:=CStr(.Cells(d,“A”).Text),Item:=vbNullString
下一个d
带.列(1)
.AutoFilter字段:=1,Criteria1:=dict.keys,运算符:=xlFilterValues
以
以
以
dict.RemoveAll:Set dict=Nothing
与其他方法相比,这似乎需要做很多工作,但对于具有复杂标准的大型数据块,这最终会更快


选项5:带有属性的伪自动筛选

这可能是最简单的解决方案。只需在行中循环,将标准与列L和AA进行比较。收集与不匹配的行,并将属性应用于属性集合


接收属性调整的示例数据行

作为代码:

    Dim crit As Range
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        .Range("AC1:AD1") = Array(.Range("L1").Value, .Range("AA1").Value)
        .Range("AC2") = "ABC"
        .Range("AD3") = "<>DEF"
        Set crit = .Range("AC1:AD3")
        With .Cells(1, 1).CurrentRegion
            .AdvancedFilter Action:=xlFilterInPlace, _
                            CriteriaRange:=crit, Unique:=False
        End With
    End With
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            With .Resize(.Rows.Count - 1, 1).Offset(1, .Columns.Count)
                .Formula = "=OR($L2=""ABC"",$AA2<>""DEF"")"
            End With
        End With
        With .Cells(1, 1).CurrentRegion
            .AutoFilter Field:=.Columns.Count, Criteria1:="TRUE"
            'clean up the 'helper' column and refresh the last_cell afterwards
            If False Then
                .Columns(.Columns.Count).EntireColumn.Delete
                .Parent.UsedRange
            End If
        End With
    End With
    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                .FormatConditions.Add Type:=xlExpression, _
                                      Formula1:="=OR($L2=""ABC"",$AA2<>""DEF"")"
                .FormatConditions(.FormatConditions.Count).SetFirstPriority
                .FormatConditions(1).Font.Color = vbRed
            End With
            .AutoFilter Field:=1, _
                        Criteria1:=vbRed, Operator:=xlFilterFontColor
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                .FormatConditions(1).Delete
            End With
        End With
    End With
    Dim d As Long, dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            For d = 2 To .Rows.Count
                If LCase(.Cells(d, "L")) = "abc" Or LCase(.Cells(d, "AA")) <> "def" Then _
                    dict.Add Key:=CStr(.Cells(d, "A").Text), Item:=vbNullString
            Next d
            With .Columns(1)
                .AutoFilter Field:=1, Criteria1:=dict.keys, Operator:=xlFilterValues
            End With
        End With
    End With

    dict.RemoveAll: Set dict = Nothing
    Dim d As Long, rng As Range

    With Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, 1).CurrentRegion
            .EntireRow.Hidden = False
            For d = 2 To .Rows.Count
                If (LCase(.Cells(d, "L").Value2) <> "abc" And _
                    LCase(.Cells(d, "AA").Value2) = "def") Then
                    If rng Is Nothing Then
                        Set rng = .Rows(d)
                    Else
                        Set rng = Union(rng, .Rows(d))
                    End If
                End If
            Next d
            rng.EntireRow.Hidden = True
        End With
    End With
Dim d为长,rng为范围
带工作表(“表1”)
如果.AutoFilterMode,则.AutoFilterMode=False
带.Cells(1,1).CurrentRegion
.EntireRow.Hidden=