宏循环遍历每个工作表并取消过滤所有表

时间:2014-07-02 14:19:28

标签: excel vba excel-vba

我编写了一些非常简单的代码,它只是遍历工作簿中的每个工作表,然后使用ActiveSheet.ShowAllData删除过滤(如果已激活)。

这是(更新的)代码:

 Sub removeFilters()
'This macro removes any filtering in
'order to display all of the data but it does not remove the filter arrows
On Error GoTo Errorcatch

For i = 1 To ActiveWorkbook.Worksheets.Count
If ActiveSheet.Visible = True Then
ActiveSheet.ShowAllData
End If
Next i

Exit Sub

Errorcatch:
MsgBox Err.Description

End Sub

代码似乎正常工作,但它以错误结束,然后显示一个MsgBox。这是MsgBox的截图:

Error

工作簿有一些隐藏的工作表,我想完全跳过它们,这就是我添加If语句的原因。

3 个答案:

答案 0 :(得分:0)

如果未激活自动过滤,则ShowAllData会引发错误,因此:

Dim ws As Worksheet

On Error Resume Next
For Each ws In ThisWorkbook.Worksheets
    If ws.Visible Then ws.ShowAllData
Next ws
on error goto 0     'or to hell or whatever

答案 1 :(得分:0)

如果您正在处理表格:

Sub removeFilters()
    Dim ws As Worksheet
    Dim oList As ListObject
'This macro removes any Table filtering in
'order to display all of the data but it does not remove the filter arrows

    For Each ws In ActiveWorkbook.Worksheets
        For Each oList In ws.ListObjects
            oList.AutoFilter.ShowAllData
        Next oList
    Next ws

End Sub

答案 2 :(得分:0)

这完全适合我。

    Sub resetFilter()


    Dim xWs As Worksheet
    For Each Wks In ThisWorkbook.Worksheets
    On Error Resume Next
    If Wks.AutoFilterMode Then
        Wks.AutoFilter.ShowAllData
    End If
    Next Wks
End Sub
相关问题