1004未找到任何单元-错误处理

时间:2019-03-18 15:48:34

标签: excel vba debugging

任何人都可以通过以下代码来建议如何处理“未找到任何单元”错误。这是一个较大的子程序的一部分,该子程序可能通常不返回任何值,但是按以下方式处理错误(对我的许多其他情况都适用)仍然返回“运行时错误'1004':找不到单元格”。我在做什么错了?

On Error GoTo Error_Exit_3
    Range("Q:Q").SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
Error_Exit_3:

3 个答案:

答案 0 :(得分:4)

我要做什么:

saveAssignment(formValues) {
    this.rulesService.validate(3, 1).pipe(
      switchMap((isValid) => {
           if(isValid){
                  return this.assignmentService.saveAssignment(formValues)
           }else{
                  return of(false);  // false or whatever the value you want to have
           }
      })
    ).subscribe((response) => {
        if(response === false){
            alert('Invalid'); // alert('came in here instead');
        }else{
            console.log(response);  // response of this.assignmentService.saveAssignment(formValues)  
        }
    })

答案 1 :(得分:3)

您还可以将其设置为范围:

Sub t()
Dim cellsWithErroringFormulas As Range

On Error Resume Next
Set cellsWithErroringFormulas = Range("Q:Q").SpecialCells(xlCellTypeFormulas, 16)
On Error GoTo 0

If cellsWithErroringFormulas Is Nothing Then
   ' Do whatever
    MsgBox ("No formulas result in an error!")
    Exit Sub
ElseIf cellsWithErroringFormulas.Rows.Count > 0 Then
    cellsWithErroringFormulas.SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
    ' Now, if you call `cellsWithErroringFormulas` again, it will error since you removed all those references.
    ' So to be explicit, let's clear that variable.
    Set cellsWithErroringFormulas = Nothing
End If

End Sub

我调整了变量名,只是因为您并不是从技术上寻找带有公式的行,而是在寻找带有公式的单元格导致错误。这里有点笨拙,请根据需要重命名。只是想指出这一点。

此外,由于我不知道您下一步打算做什么,因此我添加了Set cellsWithErroringFormulas = Nothing,因为删除错误行后我们无法使用该引用。您可能不需要,但我也想指出这一点。

答案 2 :(得分:0)

请检查下面的链接。它已经在这个论坛中回答了。 另外,请访问此论坛的规则。:)

1004 Error: No cells were found, easy solution?