使用多个条件查找所有结果

时间:2012-08-23 23:03:19

标签: vba excel-vba excel-formula excel

我有一个包含多列的表格,我希望使用标准过滤表格并使用匹配项接收范围。 (1)我知道我可以使用循环轻松地在表格中进行迭代,或者(2)我可以在列中添加过滤器。

我不喜欢(1)因为表中的迭代太慢了,但我可以这样做。

Excel是否有一个函数返回一步过滤了某个条件的范围?类似'function multipleVlookup(...)As Range'

修改 答案后我的代码:(感谢Alexandre)

Set tableRange = Range("A1:M" & lastRow)

' Filter 
With tableRange
    Call .AutoFilter(5, "test1")
    Call .AutoFilter(11, "test2")
    Call .AutoFilter(9, myDate)
End With

Set filteredRange = tableRange.SpecialCells(xlCellTypeVisible)

' Disable Filter
With tableRange
    Call .AutoFilter(5)
    Call .AutoFilter(11)
    Call .AutoFilter(9)
End With

' Do something with this result
For Each c In f.Cells.Rows
    actualRow = c.Row
    If actualRow <> 1 Then
        ' Do something
    End If
Next

1 个答案:

答案 0 :(得分:3)

如果您可以过滤数据,则可以使用表格Range并像这样调用SpecialCells方法:

Dim table_range as Range
Dim filtered_range as Range

Set table_range = Range(...)
table_range.AutoFilter field:=... criteria1:=...
Set filtered_range = table_range.SpecialCells(xlCellTypeVisible)

这将返回一个Range对象,其中只包含原始范围的可见单元格。