尝试使用Range确定Cell是否等于Value,得到运行时错误1004

时间:2014-02-04 17:32:43

标签: excel vba excel-vba excel-2010

With wbImportFile.Sheets(1)
    .Range(Cells(iFirstRow + 464, iFirstColumn)).Select
    If Not Selection.Value = "" Then
        MsgBox ("This cam has an offset")
    End If
End With

全部,我正在尝试确定我导入的文件中的特定单元格是否具有值。 wbImportFile是一个Workbook变量,iFirstRow和iFirstColumn是Long变量(在前面的代码中计算)。

我只是想确定位于iFirstRow +第464行和iFirstColumn的单元格是否为空,当我这样做时,我在第二行出现错误,即运行时错误1004“应用程序定义或对象定义错误。“

我还尝试了没有with语句的代码:

If Not Range(Cells(iFirstRow + 464, iFirstColumn)).Value = "" Then
    MsgBox ("This cam has an offset")
End If

但我得到同样的错误。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

试试这段代码:

With wbImportFile.Sheets(1)
    If Not .Cells(iFirstRow + 464, iFirstColumn).Value = "" Then
        MsgBox "This cam has an offset"
    End If
End With

您可以使用此RangeRange("A1:A10")之类的Range(Cells(1,1),Cells(10,1))对象,但不能以这种方式使用Range对象:.Range(Cells(..))。您只需使用.Cells()代替。