为什么当表单有filter时,ActiveSheet.FilterMode返回False?

我使用下面的代码试图检测应用于表中的列的filter,然后清除filter:

If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData 

根据微软文件:

如果工作表包含有隐藏行的已过滤列表,则此属性为true。

这似乎并不是这种情况,因为ActiveSheet.Filtermode仅在select应用filter的表格中的单元格时返回True

  • 第一个问题:文档是否错误? 文档

  • 第二个问题:我唯一的select是在表格内select一个单元格来让expression式返回True吗?

PS我正在使用Excel 2010

编辑:回答问题2,非基于方法来清除筛选器…

If ActiveSheet.ListObjects(1).Autofilter.FilterMode Then ActiveSheet.ListObjects(1).Autofilter.Showalldata

我可以在Excel 2013上同时复制这两个问题: FilterMode上的Bug FalseShowAllData上的错误。

作为对文档是否错误的回应,我会说它缺less一个资格来说ActiveCell应该在ListObjectDataBodyRange 。 也许文档是正确的,但这是一个没有得到解决的错误。 也许你可以更新你的问题与文档的链接?

回答第二个问题 – 我同意使用这种解决方法是最明显的解决scheme。 使用Select似乎有些不愉快,但有时候我猜这是无法避免的。

这是我如何使用Intersect函数来检查它ActiveCell当前在ListObjectDataBodyRange的区域:

 Option Explicit Sub Test() Dim rng As Range Dim ws As Worksheet Dim lst As ListObject 'get ActiveCell reference Set rng = ActiveCell 'get reference to Worksheet based on ActiveCell Set ws = rng.Parent 'is there a Listobject on the ActiveCells sheet? If ws.ListObjects.Count > 0 Then Set lst = ws.ListObjects(1) Else Debug.Print "No table found" Exit Sub End If 'is cell is in the DataBodyRange of ListObject? If Intersect(rng, lst.DataBodyRange) Is Nothing Then 'set the ActiveCell to be in the DataBodyRange lst.DataBodyRange.Cells(1, 1).Select End If 'now you can safely call ShowAllData If ws.FilterMode = True Then ws.ShowAllData End If End Sub 

编辑

关于@奥森的评论:

如果你跳过如果相交(rng,lst.DataBodyRange)是Nothing然后使用If lst.AutoFilter.FilterMode Then lst.AutoFilter.ShowAllData End If?

所以,你可以检查ListObject本身的FilterMode ,然后只要你有一个ListObject的引用,你可以使用他的代码:

 If lst.AutoFilter.FilterMode Then lst.AutoFilter.ShowAllData End If 

一个更简单的select可以是只是AutoFit所有行:

 Rows.AutoFit 

与此相关的问题是,它将取消隐藏和自动适应活动工作表上的所有行。


http://www.contextures.com/excelautofilterlist.html更新

 Dim list As ListObject For Each list ActiveSheet.ListObjects If list.AutoFilter.FilterMode Then list.AutoFilter.ShowAllData End If Next