Excel 2013 VBA清除所有过滤器宏

似乎旧的宏不工作了。我有适当的安全设置来运行VBA宏,但当我尝试了一些方法来清除一个工作表上的所有过滤器时,我得到了一个编译错误。

以下是我所尝试的。


Sub AutoFilter_Remove()
'这个宏删除了任何过滤,以便显示所有的数据,但它没有删除过滤器的箭头
ActiveSheet.ShowAllData
End Sub

我在工作表上设置了清除所有过滤器的按钮,以方便用户使用,因为工作表有很多列都有过滤器。

试试这个。

If ActiveSheet.AutoFilterMode Then ActiveSheet.ShowAllData
评论(3)
解决办法

如果床单上已经有了一个过滤器,那么。

Sub Macro1()
    Cells.AutoFilter
End Sub

将删除它。

评论(1)

试试这样的方法。

Sub ClearDataFilters()
'Clears filters on the activesheet. Will not clear filters if the sheet is protected.
On Error GoTo Protection
If ActiveWorkbook.ActiveSheet.FilterMode Or _
   ActiveWorkbook.ActiveSheet.AutoFilterMode Then _
   ActiveWorkbook.ActiveSheet.ShowAllData

Exit Sub
Protection:
If Err.Number = 1004 And Err.Description = _ 
    "ShowAllData method of Worksheet class failed" Then
    MsgBox "Unable to Clear Filters. This could be due to protection on the sheet.", _
    vbInformation
End If

End Sub

如果工作表处于过滤模式,.FilterMode返回true。(见此了解更多信息。) 关于`.AutoFilter'的更多信息,请看这里
最后,this将提供关于.ShowAllData方法的更多信息。

评论(0)