Using an array for autofilter criteria

我的梦境 提交于 2019-12-23 23:10:35

问题


I have the below code which will delete rows based on criteria in column I:

Sub Strip()
    Dim rng As Range

    With ActiveSheet
        .Columns("I").AutoFilter Field:=1, Criteria1:="=70-79%", VisibleDropDown:=False
        Set rng = .AutoFilter.Range
    End With
    If rng.Columns("I").SpecialCells(xlCellTypeVisible).Count - 1 > 0 Then
        Application.DisplayAlerts = False
        rng.Offset(1, 0).SpecialCells(xlCellTypeVisible).Delete
        Application.DisplayAlerts = True
    End If
    rng.AutoFilter
End Sub

I have about 100 different criteria that I want to act on in this way. I'd rather not have to repeat this code 100 times, and so can anyone tell me how to code this in the form of an array? I've tried various methods but can't seem to get it to work.


回答1:


Use

.Columns("I").AutoFilter Field:=1, Criteria1:=MyArray,  Operator:=xlFilterValues

Where MyArray is a string Array

Example

Dim MyArray(1 To 4) As String

MyArray(1) = "This"
MyArray(2) = "is"
MyArray(3) = "an"
MyArray(4) = "array"

'
'~~> Rest of code
'

.Columns("I").AutoFilter Field:=1, Criteria1:=MyArray, Operator:=xlFilterValues

'
'~~> Rest of code
'

Screenshot



来源:https://stackoverflow.com/questions/44600448/using-an-array-for-autofilter-criteria

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!