简体   繁体   中英

Filter criteria based on cell value

I want to filter a range based on a cell value:

It works:

ThisWorkbook.Worksheets("Cost Table").Range("$A$38:$O$68").AutoFilter Field:=2, Criteria1:=Array("LATAM", "NOAM"), Operator:=xlFilterValues

Doesn't work:

ThisWorkbook.Worksheets("Cost Table").Range("$A$38:$O$68").AutoFilter Field:=2, Criteria1:=ThisWorkbook.Worksheets("Cost Table").Range("Q100").Value, Operator:=xlFilterValues

Q100 = "LATAM", "NOAM"

What is the desired input for criteria?

您可以将LATAM放在Q100中, NOAM Q101中的NOAM放在Q101中(均不带引号),并使用Criteria1:=ThisWorkbook.Worksheets("Cost Table").Range("Q100:Q101").Value作为条件。

You have to create an array if you want to apply more that one value.

dim crit() as string
crit = split(ThisWorkbook.Worksheets("Cost Table").Range("Q100").Value, ",")
ThisWorkbook.Worksheets("Cost Table").Range("$A$38:$O$68").AutoFilter Field:=2 Criteria1:=crit, Operator:=xlFilterValues

NB If your cell Q100 contains quotes, you have to get rid of it:

 crit = split(replace(ThisWorkbook.Worksheets("Cost Table").Range("Q100").Value, """",""), ",")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM