excel - 如何修复运行时1004 : No cells were found error

标签 excel vba error-handling

下面的代码(查找最后一行)以前工作正常,但是开始使

run time error 1004



(第三行)。我在这里读过类似的文章,但想在此特定代码上修复它。
With Sheet1.Range("A:A")

    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
        ' next line is where it errors
        Set rng = .SpecialCells(xlCellTypeConstants, 1)
        Set rng = rng.Areas(rng.Areas.Count)
        lastrow = rng.Cells(rng.Cells.Count).Row
    End If

End With

最佳答案

似乎无法在.specialcells方法中捕获错误。

可以使用以下解决方法。

With Sheet1.Range("A:A")
    If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
        ' next line is where it errors
        On Error Resume Next 'Avoid Error Call
        Set Rng = .SpecialCells(xlCellTypeConstants, 1)
        On Error GoTo 0 'Reset Error Ignoring Behavior
        If Not Rng Is Nothing Then
            Set Rng = Rng.Areas(Rng.Areas.Count)
            lastrow = Rng.Cells(Rng.Cells.Count).Row
        End If
    End If
End With

关于excel - 如何修复运行时1004 : No cells were found error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58910398/

相关文章:

excel - 使用 VBA 和 IE 解析 Internet HTML 页面的数据

excel - 在单列中循环 22 行时内存不足

.net - GridView : Export to csv issue

excel - 如何使用 VS 2008 VB 将一个单元格或一系列单元格从一个 Excel 电子表格复制到另一个 Excel 电子表格

javascript - 如何让Excel VBA自动点击IE中的javascript a href链接

excel - SendKeys到selenium VBA中打开对话框中的文件名字段

excel - 根据当前日期在单元格中输入内容

php - 在 CodeIgniter 中处理数据库错误

c++ - scoped_lock() - 使用 pthread 的 RAII 实现

go - 堆栈跟踪和 golang 错误。Unwrap()