处理无过滤范围为空时发现错误的单元格

rue*_*edi 2 excel vba

我对一个范围进行一些过滤并复制过滤范围

myRange.SpecialCells(xlCellTypeVisible).Copy
Run Code Online (Sandbox Code Playgroud)

但是一旦过滤器过滤所有情况,我得到错误"1004没有找到细胞".我正在寻找一种方法来检查(没有On Error)过滤范围是否为空?

我已经尝试设置范围lastRow = .Cells(.Rows.Count, ColumnName).End(xlUp).Row并检查if lastRow > 0但是这样我也计算了filterd(或隐藏)rowcontents.

我也试过了

Sub test()
    Dim rngStart As Range
    Dim rngFiltered As Range

    Set rngStart = Sheets(1).Range("A1:A6")
    Set rngFiltered = rngStart.SpecialCells(xlCellTypeVisible).Select

    If rngFiltered.Rows.Count = 0 Then
        MsgBox ("No Cases")
    Else
        MsgBox ("Found Cases")
    End If
End Sub
Run Code Online (Sandbox Code Playgroud)

但在这里我得到了错误Set rngFiltered.Sort线也是如此.如果有人能帮助我,那会很棒.

Tim*_*ams 7

On Error Resume Next
Set rngFiltered = rngStart.SpecialCells(xlCellTypeVisible)
On Error Goto 0

If not rngFiltered is Nothing then
    rngFiltered.Copy
End If
Run Code Online (Sandbox Code Playgroud)


rue*_*edi 5

我将解决方案存储到一个函数中。在这里,我对机械强度使用错误。

Function errorCatchEmptyFilter(ByRef rngstart As Range) As Boolean

errorCatchEmptyFilter = False

'here I get an error if there are no cells
    On Error GoTo hell
    Set rngFiltered = rngstart.SpecialCells(xlCellTypeVisible)

Exit function

hell:
errorCatchEmptyFilter = True

End Function
Run Code Online (Sandbox Code Playgroud)