False
我可以在 Excel 2013上复制您的两个问题:错误FilterMode
和错误ShowAllData
。
作为对文档是否错误的回应,我会说缺少资格说 theActiveCell
应该在ListObject
sDataBodyRange
中。也许文档是正确的,但这是一个尚未解决的错误。也许您可以通过文档链接更新您的问题?
关于您的第二个问题-我同意使用此解决方法是最明显的解决方案。使用起来似乎有点不愉快,Select
但有时我想这是无法避免的。
这就是我使用该Intersect
功能检查它ActiveCell
当前是否在以下区域中的DataBodyRange
方法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
编辑
除了@orson 的评论:
如果您跳过 If Intersect(rng, lst.DataBodyRange) Is Nothing Then 并使用 If lst.AutoFilter.FilterMode Then lst.AutoFilter.ShowAllData End If 会发生什么?
所以,你可以检查FilterMode
它ListObject
本身,然后只要你有引用ListObject
你就可以使用他的代码:
If lst.AutoFilter.FilterMode Then
lst.AutoFilter.ShowAllData
End If