简体   繁体   English

在VBA中过滤Excel表

[英]Filter Excel table in VBA

I have an Excel table linked to a data validation list. 我有一个链接到数据验证列表的Excel表。 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). 过滤器是年份,因此2013、2014、2015、2016和(空白)。 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. 注意:第一列中的日期采用DD / MM / YYYY格式,因此,即使在过滤器选项中仅表示年份,我也使用通配符以防它采用完整格式。 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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