Remove Filters From Columns of an Excel Table (Workbook_SheetActivate
)
- Copy the code into the
ThisWorkbook
module.
- When activating (selecting) another worksheet, if it contains an Excel table, the filters in the first and last columns will be removed so will their dropdown arrows.
- To test it, filter a table by the first, by another, and by the last columns. After selecting another worksheet and then selecting the first worksheet, only the filter of the other column remains.
Option Explicit
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim tbl As ListObject
On Error Resume Next
Set tbl = Sh.ListObjects(1)
On Error GoTo 0
If Not tbl Is Nothing Then
With tbl.Range
.Columns(1).AutoFilter 1, VisibleDropdown:=False
.Columns(.Columns.Count).AutoFilter .Columns.Count, _
VisibleDropdown:=False
End With
End If
End Sub
Edit: Excluding Worksheets
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
' To exclude charts (kind of covered with 'On Error' statement).
'If sh.Type <> xlWorksheet Then Exit Sub
Dim Exceptions As Variant: Exceptions = Array("Sheet1", "Sheet2")
' To exclude the sheets in the list:
If IsError(Application.Match(Sh.Name, Exceptions, 0)) Then
' To restrict to the sheets in the list:
'If IsNumeric(Application.Match(Sh.Name, Exceptions, 0)) Then
Dim tbl As ListObject
On Error Resume Next
Set tbl = Sh.ListObjects(1)
On Error GoTo 0
If Not tbl Is Nothing Then
With tbl.Range
.Columns(1).AutoFilter 1, VisibleDropdown:=False
.Columns(.Columns.Count).AutoFilter .Columns.Count, _
VisibleDropdown:=False
End With
End If
End If
End Sub