Vba 排除筛选器中的值,即使该值不存在

Vba 排除筛选器中的值,即使该值不存在,vba,excel,Vba,Excel,我有很多文件要循环浏览。在每个文件中,我必须过滤掉特定的值 例如。 我必须过滤掉数字1,3,5,7,9,并保留所有其他内容 开始 1 2 3 4 9 11 15 结果 2 4 11 15 我想出了这个代码,但它被证明是无用的,因为它不工作 Rows("1:1").Select Selection.AutoFilter wkb.Sheets("temp").Range("$A$1:$AC$72565").AutoFilter Field:=9, Criteria1:="

我有很多文件要循环浏览。在每个文件中,我必须过滤掉特定的值

例如。 我必须过滤掉数字
1,3,5,7,9
,并保留所有其他内容

开始

1
2
3
4
9
11
15
结果

2
4
11
15
我想出了这个代码,但它被证明是无用的,因为它不工作

    Rows("1:1").Select
    Selection.AutoFilter
    wkb.Sheets("temp").Range("$A$1:$AC$72565").AutoFilter Field:=9, Criteria1:="Main"
    wkb.Sheets("temp").Range("$A$1:$AC$72565").AutoFilter Field:=10, Criteria1:="C"
    wkb.Sheets("temp").Range("$A$1:$AC$72565").AutoFilter Field:=11, Criteria1:="N"

'this part of the code is where I try to exclude values
    wkb.Sheets("temp").Range("$A$1:$AC$72565").AutoFilter Field:=1, Criteria1:=Array("<>1", "<>3", "<>5", "<>7", "<>9")
行(“1:1”)。选择
自动筛选
wkb.Sheets(“temp”).范围($A$1:$AC$72565”)。自动筛选字段:=9,标准1:=“Main”
wkb.Sheets(“temp”).范围($A$1:$AC$72565”)。自动筛选字段:=10,标准1:=C
wkb.Sheets(“temp”).范围($A$1:$AC$72565”)。自动筛选字段:=11,标准1:=N
'这部分代码是我尝试排除值的地方
wkb.Sheets(“temp”).范围($A$1:$AC$72565”)。自动筛选字段:=1,标准1:=数组(“1”、“3”、“5”、“7”、“9”)

考虑反向逻辑,只过滤要删除的内容

Sub test()

Dim rng As Range

Set rng = wkb.Sheets("temp").Range("$A$1:$AC$72565")

rng.AutoFilter Field:=1, Criteria1:=Array("1", "3", "5", "7", "9"), Operator:=xlFilterValues
' In case your data has header used Offset(1,0) to preserve the header on the first row
rng.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
rng.AutoFilter

End Sub
尝试以下操作(代码中的所有必要注释):

Sub filter()
'declaration of variables
Dim numbers(5) As Long, cell As Range, number As Variant
'fill this array accordingly to your needs (notee that you'd have to change array size in declaration
numbers(0) = 1
numbers(1) = 3
numbers(2) = 5
numbers(3) = 7
numbers(4) = 9
'here we loop through all cells in frist row, this lenghty formule is for finding last fill column in first row
For Each cell In Range(Cells(1, 1), Cells(1, Cells(1, Columns.Count).End(xlToLeft).Column))
    'here we loop through specified numbers to filter out these values
    For Each number In numbers
        If number = cell.Value Then
            'if match is found, clear the cell and leave the loop
            cell.Value = ""
            Exit For
        End If
    Next
Next
End Sub