I can replicate both your issues on Excel 2013: both the buggy False
on FilterMode
and the error on ShowAllData
.
In response to whether the documentation is wrong, I would say that it is missing a qualification to say that the ActiveCell
should be in the ListObject
s DataBodyRange
. Perhaps the documentation is correct but that this is a bug that has not been addressed. Maybe you can update your question with a link to the documentation?
Re your second question - I agree that using this workaround is the most obvious solution. It seems a bit unpleasant to use Select
but sometimes I guess this cannot be avoided.
This is how I did it using the Intersect
function to check it the ActiveCell
is currently in the area of the 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
Further to @orson's comment:
What happens if you skip the If Intersect(rng, lst.DataBodyRange) Is Nothing Then and use If lst.AutoFilter.FilterMode Then lst.AutoFilter.ShowAllData End If ?
So, you can check the FilterMode
of the ListObject
itself and then as long as you have a reference to the ListObject
you can use his code:
If lst.AutoFilter.FilterMode Then
lst.AutoFilter.ShowAllData
End If
ActiveSheet
refers to the currently active sheet and may differ to the one you might think is active. AddDebug.Print ActiveSheet.Name
to see which sheet VBA is referring to. Anyway, you should start coding more explicitly and should useThisWorkbook.WorkSheets("The_sheet_that_I_want").FilterMode
instead. Oh, and please don't add relevant information in comment. Rather edit your original post and add the relevant tag [Excel-2010] to your post. Afterwards, you can also delete your comment. – RalphActiveSheet.ShowAllData
without the if? My guess is that if the filter is not applied to the whole sheet but just part of it, it can return false. – SlaiActiveSheet.ShowAllData
without the If gives: "Run-time error '1004': ShowAllData method of Worksheet class failed" when cell outside the table is selected at execution, works if cell in table is selected at execution. – orsonWorksheet.ListObjects
? – Slai