很多.AutoFilter Criteria,就像OR一样

时间:2012-01-24 04:51:16

标签: excel vba excel-vba

我有两个工作表,一个用于过滤另一个工作表。

使用VBA我希望使用AutoFilter,如下所示:

Sheet 1中:

Username, Country, City
User1, America, New York
User2, America, Miami
User3, America, Los Angeles

Sheet 2中:

Country, City, Contact
America, <>Miami, AllCitiesButMiami@test.com
America, Miami, OnlyMiami@test.com

到目前为止,我的自动过滤器看起来像这样:

With ActiveSheet

    .AutoFilterMode = False 'Reset the filter

    'Set filter values
    With .Range("A1:C1")
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:=VBA.Array(Country)
        .AutoFilter Field:=2, Criteria1:=VBA.Array(City)
    End With

    'Set contact to third visible cell
    TheContact = .UsedRange.Offset(1).SpecialCells(xlCellTypeVisible).Cells(3)

    .AutoFilterMode = False 'Reset the filter

End With

迭代Sheet1时的结果应为:

Sheet1:Row1(User1):City = New York,TheContact =“AllCitiesButMiami@test.com”

Sheet1:Row2(User2):City = Miami,TheContact =“OnlyMiami@test.com”

Sheet1:Row3(User3):City = Los Angeles,TheContact =“AllCitiesButMiami@test.com”

问题: 我需要第二场比赛的帮助,“&lt;&gt;迈阿密”并没有拿到所有城市但迈阿密

1 个答案:

答案 0 :(得分:0)

这就是我解决问题的方法。在我使用Criteria“〜&lt;&gt; *”

之前,&lt;&gt;迈阿密没有被接收
....
Dim City
City = Worksheets("Sheet1").Range("C" & i) 'i = current row

'I set the filter to current row data
.AutoFilter Field:=14, Criteria1:=City

'Check row count (cell count / no. of columns)
rowCount = .Parent.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Count / 3

'If the row count is < 2, no data, change the filter criteria to find other
If (rowCount < 2) Then
    .AutoFilter Field:=5, Criteria1:="~<>*"
End If
....