vba - excel中没有依赖的单元格的 "cell.dependents.count"处理错误

标签 vba excel error-handling dependencies runtime-error

我正在尝试编写宏来自动将所选范围内的单元格分类为“输入”、“输出”和“计算”单元格。当 rcell 没有先例或依赖项时,对于包含 rCell.Precedents.Count 或 rCell.Dependents.Count 的语句,我收到“运行时错误'1004':未找到单元格”。我试过了

IsEmpty(rCell.Dependents)

但我仍然遇到同样的错误。
Private Sub test_v05()
    ActiveSheet.Cells.Interior.ColorIndex = xlColorIndexNone
    Dim Target As Range, rCell As Range
    Set Target = Sheets("Sheet1").Range("A1:C10")
    For Each rCell In Target.Cells
        If IsEmpty(rCell.Value) = True Then GoTo AllDone:

        If rCell.Precedents.Count = 0 And rCell.Dependents.Count > 0 Then
        rCell.Style = "Input"
        GoTo AllDone:
        End If

        If rCell.HasFormula And rCell.Dependents.Count = 0 Then
        rCell.Style = "Output"
        GoTo AllDone:
        End If

        If rCell.Precedents.Count > 0 And rCell.Dependents.Count > 0 Then
        rCell.Style = "Calculation"
        End If
AllDone:
    Next rCell
End Sub

在跟踪没有依赖项或单元格的先例时处理错误的最佳方法是什么?

最佳答案

考虑:

Sub Dependental()
    Dim n As Long
    n = 0

    On Error Resume Next
        n = Cells.Precedents.Count
    On Error GoTo 0

    MsgBox "there are " & n & " cells with dependents"
End Sub

关于vba - excel中没有依赖的单元格的 "cell.dependents.count"处理错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35866979/

相关文章:

C - 调试断言失败 : _CrtIsValidHeapPointer(block)

asp.net - 如何在ASP.NET MVC中使用Application_Error?

excel - 使用 Like 和通配符进行 VBA 字符串搜索

excel - 使用excel-vba删除空白列以创建数据透视表

arrays - 删除数组中的目录

Excel,链接2个单元格以具有相同的格式

vba - 根据另一个单元格值将公式写入单元格

vba - 将 <p> 和 </p> 标记附加到 vba 中一列中所有单元格中的文本。列中的单元格不断变化

r - Excel函数处理可变数量的变量和范围,然后将它们输入到R中

c# - 如何处理 ASP.NET MVC 中的 "default"异常?