应用自动过滤器后,“无单元格”:Excel VBA

时间:2016-07-22 14:08:03

标签: excel vba

我正在自动过滤一系列单元格,在这种情况下,可能存在一个实例,其中没有符合过滤条件的单元格(我正在查看的列中没有单元格为“是”)。发生这种情况时,我的程序会返回错误“No Cells”。我如何设置错误处理,以便我的程序尽管如此?我在自动过滤器行之后尝试了On Error Resume Next,但它不会到达该行,因为它卡在过滤器行上。

.Range(.Cells(1, 1), .Cells(counter, LstCol1)).AutoFilter Field:=z, Criteria1:="Yes"
    Set rngFilter_Yes = Intersect(.UsedRange, .UsedRange.Offset(1), _
    .Columns(2)).SpecialCells(xlCellTypeVisible)
    On Error Resume Next <- doesn't reach this line :( 

1 个答案:

答案 0 :(得分:0)

On Error Resume Next的使用需要在你的陈述之前如下:

On Error Resume Next ' ignores all errors and proceeds
.Range(.Cells(1, 1), .Cells(counter, LstCol1)).AutoFilter Field:=z, Criteria1:="Yes"
Set rngFilter_Yes = Intersect(.UsedRange, .UsedRange.Offset(1), _
.Columns(2)).SpecialCells(xlCellTypeVisible)
On Error Goto 0 'Returns the program to normal error handling state