VBA ShowAllData method not working properly

  Kiến thức lập trình

I have this simple routine:

Sub ClearFilter()
    Dim wsda As Worksheet
    Set wsda = ThisWorkbook.Worksheets(1)
    If wsda.FilterMode = True Then wsda.ShowAllData
End Sub

If I run it alone, it does what it is supposed to do, i.e. it clears all filter criteria activated in sheet wsda and then all data are shown. This happens after some tenths of seconds after the start of the run.

However, if I call the same routine from another excel function (not sub) included in the same module the applied filters are not cleared, even when the function execution is finished.

I spent the whole day making attempts and reading MS VB manual and more but I was not able to find a solution. Could anyone help me to understand why the ShowAllData method is not working in the second case? And then suggest me how to make it work?
I need to clear filters because otherwise the countifs functions in my code do not work properly.

LEAVE A COMMENT