【问题标题】:How to clear the filter that been set in the vba code?如何清除在 vba 代码中设置的过滤器?
【发布时间】:2013-02-08 17:15:58
【问题描述】:

我有一个 Excel VBA 项目,用于发送电子邮件,其中包含用户在工作表 1 中选择的文档。在工作表 1 中有两列:列 1 和列 2。列 2 将所有文档列为超链接,如果用户想要选择一个文档,他/她在 Column1 中为该文档添加一个“x”。然后用户单击发送按钮发送电子邮件。下面是按钮点击事件的代码:

…
    Set Ash = ActiveSheet
    On Error GoTo cleanup

            Ash.Range("A5:B500").AutoFilter Field:=1, Criteria1:="x"

            For Each cell In Ash.Range("B5:B300").SpecialCells(xlCellTypeVisible)
                If cell.Offset(0, -1).Value = "x" Then
                    …
                End If
            Next
…
cleanup:
    Set OutApp = Nothing
    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With
 …

当出现错误时,我的问题就出现了。用户点击发送按钮后,如果出现错误,Sheet1 只选择了那些文档,其他文​​档都被过滤掉了。每列的顶部都会显示一个过滤器按钮,这很好,因为我确实希望用户意识到出现问题。

我的问题是我无法更改过滤器设置或退出过滤器。我必须关闭 Excel 然后重新打开它。有什么方法可以通过单击 Column1 顶部的过滤器按钮来清除过滤器并将所有其他文档带回来?

【问题讨论】:

标签: excel filter vba


【解决方案1】:

这样的?

Sub ToggleAutofilter()
    ' check if autofilter is currently off
    If ActiveSheet.FilterMode = False Then
        ' if it's off, turn it on, and filter on "Nick"
        Range("d5").AutoFilter Field:=1, Criteria1:="Nick"
    Else
        ' if it's on, turn it off
        Range("d5").AutoFilter
    End If
End Sub

看到这个link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多