简体   繁体   中英

Filter Excel table in VBA

I have an Excel table linked to a data validation list. I have the table filtered to remove blanks. If I select a different item from the data validation list it refreshes the data but cuts out most of it until I refresh the filter. I'm trying to come up with a macro that I can add to my current code to refresh the filter. the filters are years, so 2013,2014,2015,2016 and (blanks). Here's what I've got so far but it just filters out everything

Sub sbFilterTable()

ActiveWorkbook.Sheets("Sheet4").ListObjects("Table2").Range.AutoFilter field:=1, _
Criteria1:="*013", Criteria2:="*014", Criteria3:="*015", Criteria4:="*016"

End Sub

Note: The dates in the first column are in DD/MM/YYYY format so I used a wildcard incase it picked up the full format, even though in the filter options it just says the year. any guidance or advice is appreciated!

Why not simply

ActiveWorkbook.Sheets("Sheet4").ListObjects("Table2").Range.AutoFilter _
    Field:=1, Criteria1:=">=1/1/2014", Operator:=xlAnd, Criteria2:="<=12/31/2016"

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