VBA:如果Range(X)中存在非空单元格,请继续执行代码。否则,结束

时间:2015-04-06 20:15:33

标签: excel vba excel-vba if-statement

我试图将一个Try-Catch类型的代码行放入我在VBA中的程序中。我得到的是下面的内容。但是,此代码只是立即运行到End If,无论列A的单元格中是否有数据。我该怎么做才能解决这个问题?
谢谢。

ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=11, Criteria1:= _
    "0"
If Range("A:A").SpecialCells(xlCellTypeVisible).Text = "<>0" _
Then
(other code)
(other code)
(other code)
End If

1 个答案:

答案 0 :(得分:1)

查看范围是否包含任何可见数据:

Sub hfksjdfh()
    Dim wf As WorksheetFunction, r As Range
    Set wf = Application.WorksheetFunction
    Set r = Range("A:A").Cells.SpecialCells(xlCellTypeVisible)

    If wf.CountA(r) > 0 Then
        MsgBox "There is at least one visible cell in column A with data"
    Else
        MsgBox "The visible part of column contains no data"
    End If
End Sub