0

我有这个文件,它根据来自另一个页面的值进行过滤。我的问题是如果列表列中没有值,是否可以忽略字段?

现在,如果未填写所有列表值,则会发生错误。列表值是照片中的 Criteria_C 等。

谢谢

另外,我如何让它显示多个结果?

Sub Proto()


Dim cList As Variant
Dim ccList As Variant
Dim uList As Variant
Dim towList As Variant
Dim aList As Variant
Dim pmList As Variant

Dim wsP As Worksheet
Dim wsL As Worksheet
Dim rng_C As Range
Dim rng_CC As Range
Dim rng_U As Range
Dim rng_ToW As Range
Dim rng_A As Range
Dim rng_PM As Range
Dim rng_Projects As Range

Set wsP = Worksheets("Projects")
Set wsL = Worksheets("Lists")
Set rng_Projects = wsP.Range("$B$6").CurrentRegion

Set rng_C = wsL.Range("List_C")
Set rng_CC = wsL.Range("List_CC")
Set rng_U = wsL.Range("List_U")
Set rng_ToW = wsL.Range("List_ToW")
Set rng_A = wsL.Range("List_A")
Set rng_PM = wsL.Range("List_PM")

cList = rng_C.Value
ccList = rng_CC.Value
uList = rng_U.Value
towList = rng_ToW.Value
aList = rng_A.Value
pmList = rng_PM.Value

rng_Projects.AutoFilter Field:=2, Criteria1:=Application.Transpose(cList)
rng_Projects.AutoFilter Field:=3, Criteria1:=Application.Transpose(ccList)
rng_Projects.AutoFilter Field:=4, Criteria1:=Application.Transpose(uList)
rng_Projects.AutoFilter Field:=5, Criteria1:=Application.Transpose(towList)
rng_Projects.AutoFilter Field:=7, Criteria1:=Application.Transpose(aList)
rng_Projects.AutoFilter Field:=8, Criteria1:=Application.Transpose(pmList), Operator _
    :=xlFilterValues
End Sub

在此处输入图像描述

4

1 回答 1

1

您可以在将数组设置为过滤器之前检查数组中是否包含值。例如,如果您想跳过没有任何值的“Criteria_C”,您可以使用以下代码:

If Not IsEmpty(cList) Then rng_Projects.AutoFilter Field:=2, _
                              Criteria1:=Application.Transpose(cList)

如果表中的数据与您指定的所有过滤条件相匹配,您的代码也应该显示多个结果。如果您想使用多个标准(例如“FEMA”和“AU”),则必须在宏执行之前扩展“List_C”范围。

于 2013-08-02T19:19:55.970 回答