Filtering Data using a macro in Excel [duplicate]

2019-09-20 05:05发布

问题:

This question already has an answer here:

  • Filtering Out Specific Value 1 answer

I have a line of code to filter out data using a macro in excel. I am wanting it to filter out two different criteria on one column. Please see the code below. From looking on google, it should work but it isnt..

Sub Unmet_Projects()

With Sheet1
    .AutoFilterMode = False
    .Range("A1:CA1").AutoFilter
    .Range("A1:CA1").AutoFilter Field:=3, Criteria1:="Fulfilled", Operator:=xlAnd, Criteria2:="Requested", VisibleDropDown:=False                    
End With

End Sub

回答1:

I believe it should be Operator:=xlOr and not Operator:=xlAnd since the values in column 3 (column C) can be only Fulfilled or Requested.

If you wish to include more values in the filter then I'd recommend the following instead:

.Range("A1:CA1").AutoFilter Field:=3, Criteria1:=Array("Fulfilled", "Requested", "Partially Assigned", "Not yet assigned", "Assigned", "etc..."), Operator:=xlFilterValues