简体   繁体   English

应用自动筛选后,“无单元格”:Excel VBA

[英]'No Cells' After applying autofilter: Excel VBA

I am autofiltering a range of cells in which case there might be an instance where there are no cells that meet the criteria of the filter (there are no cells with 'Yes' in the column I am looking at). 我正在自动过滤一系列单元格,在这种情况下,可能会出现一个实例,其中没有符合过滤条件的单元格(我正在查看的列中没有带有“是”的单元格)。 When this happens, my program returns the error 'No Cells.' 发生这种情况时,我的程序将返回错误“ No Cells”。 How can I set up error handling so that my program proceeds despite this? 如何设置错误处理,以便我的程序尽管如此仍能继续? I have tried On Error Resume Next after the autofilter line, but it will not reach that line because it is stuck on the filter line. 我在自动过滤器行之后尝试了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 :( 

The use of On Error Resume Next needs to preceed your statement like this: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM