VBA过滤超过2个条件

时间:2019-01-07 18:33:32

标签: excel vba filter

我正在寻找一种简单的解决方案来解决我遇到的过滤问题。我正在尝试过滤字段9中的以下字母组合: Function RunningCntOfOccsInArr(Source_Array As Variant, RowIndex As Long, ColIndex As Integer) As Long Dim dict As Object ' edit: corrected var spelling If IsArray(Source_Array) = False Then Exit Function ElseIf IsArrayAllocated(Source_Array) = False Then Exit Function ElseIf (RowIndex < LBound(Source_Array, 1)) Or (RowIndex > UBound(Source_Array, 1)) Then Exit Function ElseIf (ColIndex < LBound(Source_Array, 2)) Or (ColIndex > UBound(Source_Array, 2)) Then Exit Function End If Set dict = CreateObject("Scripting.Dictionary") ReDim OutPut_Array(LBound(Source_Array, 1) To UBound(Source_Array, 1), 1 To 1) For RowIndex = LBound(Source_Array, 1) To UBound(Source_Array, 1) dict(Source_Array(RowIndex, ColIndex)) = dict(Source_Array(RowIndex, ColIndex)) + 1 OutPut_Array(i, 1)(RowIndex, 1) = dict(Source_Array(RowIndex, ColIndex)) Next RowIndex RunningCntOfOccsInArr = OutPut_Array End Function RBRC 。 我尝试了几种方法。我尝试的第一种方法是:

RG

这失败了,所以我尝试使用 array ,但这似乎也失败了。它返回时只过滤了 Set Bigpic = Workbooks.Open("S:\NFInventory\groups\CID\CID Database\BigPic Files\BigPic 2018.xlsx", , , , , , , , True) lrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row Rows("1:1").Select Selection.AutoFilter ActiveSheet.Range("A1:CU" & lrow).AutoFilter Field:=9, Criteria1:="<>RB", Operator:=xlAnd, Criteria2:="<>RC", Operator:=xlAnd, Criteria3:="<>RG" ,而其他过滤器保留下来。向正确方向的任何推动都会有所帮助。

RG

4 个答案:

答案 0 :(得分:5)

您不能使用超过2个不相等。用不相等的值构建一个字典或数组列表,并将其用作带有xlfiltervalues的数组过滤器。

dim i as long, lr as long, arr as object

set arr = createobject("scripting.dictionary")

with ActiveSheet

     lr = .Cells(Rows.Count, 1).End(xlUp).Row

     for i=2 to lr
         select case ucase(.cells(i, "I").value2)
             case "RB", "RC", "RG"
                 'do nothing
             case else
                 arr.item(.cells(i, "I").value2) = vbnullstring
         end select
     next i

     .Range("A1:CU" & lr).AutoFilter Field:=9, criteria1:=arr.keys, operator:=xlfiltervalues

end with

答案 1 :(得分:1)

如果在表中包含标准不是问题(可以隐藏)

enter image description here

在代码中,您可以将条件放入隐藏表中

Range("C1:D9").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
    Sheets("Sheet2").Range("G1:I2"), Unique:=False      

答案 2 :(得分:0)

我已经通过颜色过滤手动排除了2个以上的标准。如果您的表未定义填充,则此代码将起作用。

ActiveSheet.Range("A1:CU" & lrow).AutoFilter Field:=9, Criteria1:=Array("RB", _
    "RC", "RG"), Operator:=xlFilterValues
ActiveSheet.Range("A1:CU" & lrow).SpecialCells(xlCellTypeVisible).Interior.Color = 1 
' 1 is a black fill, change to your preference
ActiveSheet.Range("A1:CU" & lrow).AutoFilter Field:=9, Operator:= _
    xlFilterNoFill
'ActiveSheet.Range("A1:CU" & lrow).Autofilter Field:=9 
'this code will unfilter the range, can be used for testing to confirm everything you  _ 
expected to filter was filtered

答案 3 :(得分:0)

难道不可以只创建一个Filter列并在其中添加if语句吗?像这样:

=IFERROR(FIND("RB",UPPER(C9)),0)+IFERROR(FIND("RC",UPPER(C9)),0)+IFERROR(FIND("RG",UPPER(C9)),0)

,然后对该列= 0进行过滤,以排除所有RB,RC,RGs