【问题标题】:Skip code when filters applied and returns no results VBA应用过滤器时跳过代码并且不返回任何结果 VBA
【发布时间】:2016-07-04 19:37:47
【问题描述】:

我正在使用以下代码将数据过滤为更大代码的一部分,但是在某些情况下,应用过滤器时可能不会返回任何结果。或者,如果没有返回结果,我想跳过部分代码。

谁能建议我为此需要使用的代码。 (我猜这将是一个 IF 语句,但不确定)

我目前使用的代码是

Worksheets("DC Allocations Input").Activate
ActiveSheet.Unprotect Password:="Projects123"
ActiveSheet.Range("$B$9:$CT$382").AutoFilter Field:=1, Criteria1:="<>"
ActiveSheet.Range("$B$9:$CT$382").AutoFilter Field:=5, Criteria1:="=OPP", Operator:=xlOr, Criteria2:="=RSK"
ActiveSheet.Range("$B$9:$CT$382").AutoFilter Field:=6, Criteria1:="<>"

任何帮助将不胜感激。

谢谢

【问题讨论】:

标签: vba excel filtering


【解决方案1】:

您可以使用带有特殊单元格的 if 语句,如下所示:

Dim lngCnt As Long
On Error GoTo someplace 'you get an error if there are no visible cells, use it to skip to where you want
lngCnt = Range("$b$10:$CT$382").SpecialCells(xlCellTypeVisible).Count 'Note I'm not including the headings row, this will always be visible
On Error GoTo 0
'Put your code to run if there are results from the filter here
someplace:
'And the code you want to resume at here

【讨论】:

    猜你喜欢
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多