下面的代码(查找最后一行)以前工作正常,但是开始使
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/