简体   繁体   中英

Excel VBA - How can I create (not apply) a filter in a range?

I have multiple "tables" in the same sheet, I have a dropdown list (Classes, students, teachers) that I use as a menu so when its changed it "redirects" to the cell where the table is in the same sheet, so if I select Classes it goes to cell A2, if I select students it goes to cell A200 and if I select teachers it goes to cell A400. Since I can create only one filter per sheet, how can I create a filter on the table that has been selected, so if I select Students, a filter is created on the range A2:P350.

This is so when the user selects an option, he doesn't have to manually create a filter on the range but it is created automatically.

I tried Hoja1.Range("A2:P350").AutoFilter, but it doesn't create the filter.

This will filter the second column of the range and select all the values equal to "abc" ?

Hoja1.Range("A2:P350").AutoFilter Field:=2, Criteria1:="abc"

You can record a macro while you manually apply the filter to see what the macro recorder does, then create your own macro using the recorded one as inspiration.

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