我使用以下代码尝试检测应用于表中列的过滤器,然后清除过滤器:
If ActiveSheet.FilterMode Then ActiveSheet.ShowAllData
根据微软文档:
如果工作表包含其中包含隐藏行的筛选列表,则此属性为 true。
自从那以后情况似乎并非如此ActiveSheet.Filtermode
只返回True
如果选择了表格内应用过滤器的单元格。
PS我使用的是Excel 2010
编辑:回答问题 2,基于非选择的清除过滤器的方法...
If ActiveSheet.ListObjects(1).Autofilter.FilterMode Then ActiveSheet.ListObjects(1).Autofilter.Showalldata
我可以在 Excel 2013 上复制您的两个问题:两个都有问题False
on FilterMode
和错误ShowAllData
.
对于文档是否有误,我想说的是missing有资格说ActiveCell
应该在ListObject
s DataBodyRange
。也许文档是正确的,但这是一个尚未解决的错误。也许您可以通过文档链接来更新您的问题?
关于你的第二个问题 - 我同意使用此解决方法是最明显的解决方案。使用起来似乎有点不爽Select
但有时我想这是无法避免的。
这就是我使用的方法Intersect
函数来检查它ActiveCell
目前位于该地区DataBodyRange
of the ListObject
:
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
Edit
进一步@orson 的评论:
如果您跳过 If Intersect(rng, lst.DataBodyRange) Is Nothing Then 并使用 If lst.AutoFilter.FilterMode Then lst.AutoFilter.ShowAllData End If ,会发生什么?
因此,您可以检查FilterMode
of the ListObject
本身然后只要你有一个参考ListObject
你可以使用他的代码:
If lst.AutoFilter.FilterMode Then
lst.AutoFilter.ShowAllData
End If
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)