IsEmpty()抛出运行时错误

时间:2013-12-18 04:57:00

标签: vba excel-vba excel-2007 excel

我想要获取的是以下代码中空单元格之前的计数:

Dim l As Long
    Dim r As Long
    For l = 0 To 65866

    If IsEmpty(wb.Worksheets("Fund Position(URIL)").Range("A1")) Then
       Exit For
    Else
      r = r + 1
   End If
    Next l

    MsgBox r

上面的代码在执行Automation error语句时会向Runtime error : -2147221080 (800401a8)投出一个if

请帮忙!

更新

我改变了

If IsEmpty(wb.Worksheets("Fund Position(URIL)").Range("A1")) Then

为:

If IsEmpty(wb.Sheets("Fund Position(URIL)").Range("A1")) Then

它有效。有人可以解释我为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

这是你在尝试的吗?

Sub Sample()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim l As Long

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Fund Position(URIL)")

    For l = 1 To 65866
        With ws
            If IsEmpty(.Range("A" & l)) Then Exit For
        End With
    Next l

    MsgBox l
End Sub