按照我在上面注释中的建议,尝试下面的代码,代码注释中的解释:
修改代码
Option Explicit
Sub UnselectCritera()
Dim inputSheet As Worksheet
Dim mainSheet As Worksheet
Dim rngCrit As Range
Dim rngOrders As Range
' Dictionary variables
Dim Dict As Object, Key As Variant
Dim AnimalArr() As String, ArrIndex As Long, LastRow As Long, i As Long
Set inputSheet = Worksheets("raw")
Set mainSheet = Worksheets("Main")
Set rngOrders = inputSheet.Range("$A$1").CurrentRegion
Set rngCrit = mainSheet.Range("rngAnimals")
' use a Dictionary ro save unique Order numbers
Set Dict = CreateObject("Scripting.Dictionary")
Application.ScreenUpdating = False
With inputSheet
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
ArrIndex = 1
ReDim AnimalArr(1 To 10000) ' redim to very large number >> will optimize later
For i = 1 To LastRow
If Not Dict.Exists(.Range("A" & i).Value2) Then ' current animal not in Dictionary >> add it as key
' check also that Animal is not in the second list
If IsError(Application.Match(.Range("A" & i).Value2, rngCrit, 0)) Then ' Match failed >> not in second list of animals
Dict.Add .Range("A" & i).Value2, .Range("A" & i).Value ' add Order number / Customer / Status
' array of numbers
AnimalArr(ArrIndex) = .Range("A" & i).Value2
ArrIndex = ArrIndex + 1
End If
End If
Next i
ReDim Preserve AnimalArr(1 To ArrIndex - 1) ' resize array to populated size
End With
' Filter according to Animal array (excluding the animals in the second list)
rngOrders.AutoFilter Field:=1, Criteria1:=AnimalArr, Operator:=xlFilterValues
Application.ScreenUpdating = True
End Sub