有没有办法在Excel VBA中不是条件数组?

时间:2015-09-16 14:15:14

标签: excel vba excel-vba

我有一个已知值的列。 5是石头写的,奇怪的是新的。我需要过滤新的(未知)值。如果我只有2个已知值,我可以过滤出Criteria1& 2通过使用=“<>”

因为我们不能使用Criteria3,4 ...... 有没有办法使用类似的东西:

Criteria1:=“<>”数组(“Val1”,“Val2”,......)

(澄清:我想展示不在数组中的所有内容。)

感谢。

1 个答案:

答案 0 :(得分:0)

使用以下数据:

enter image description here

我们希望看到 Val1 Val2 Val3

之外的所有内容

这是一种方法:

Sub AllButThree()
   Dim N As Long, ary(), c As Collection
   Dim i As Long, a As String, v As String
   ary = Array("Val1", "Val2", "Val3")
   Set c = New Collection
   N = Cells(Rows.Count, "A").End(xlUp).Row

   On Error Resume Next
   For i = 2 To N
      v = Cells(i, 1).Value
      If v <> ary(0) And v <> ary(1) And v < ary(2) Then
         c.Add v, CStr(v)
      End If
   Next i

   On Error GoTo 0
   ReDim bry(1 To c.Count)
   For i = 1 To c.Count
      bry(i) = c.Item(i)
   Next i

   ActiveSheet.Range("$A$1:$A" & N).AutoFilter Field:=1, Criteria1:=(bry), Operator:=xlFilterValues
End Sub

它构建了我们想要看到的数组。