我有这个文件根据另一个页面的值进行过滤。我的问题是,如果列表列中没有值,有一种方法可以忽略字段吗?
现在,如果未填写所有列表值,则会发生错误。列表值是照片中的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
答案 0 :(得分:1)
在将数组设置为过滤器之前,您可以检查数组中是否包含值。例如,如果您想跳过“Criteria_C”,如果它没有任何值,您可以使用以下代码:
If Not IsEmpty(cList) Then rng_Projects.AutoFilter Field:=2, _
Criteria1:=Application.Transpose(cList)
如果表中的数据符合您指定的所有过滤条件,则您的代码也应显示多个结果。如果要使用多个条件(例如“FEMA”和“AU”),则必须在宏执行之前展开“List_C”范围。