错误检查和跟踪空单元格

时间:2012-07-02 19:01:18

标签: excel vba

在Excel中,可以找到引用空单元格的公式。

例如

A1 = 1

B1 =空

C1 = .5

D1 = A1 + B1 + C1

D1将正确计算该值为1.5 但是,错误检查将识别公式D1中存在错误,并且可以向此单元格绘制箭头。

我需要提醒用户他们是否有空单元格的引用。

当我尝试使用该功能录制自己时,我得到了这个

With Application.ErrorCheckingOptions
    .EvaluateToError = False
    .TextDate = False
    .NumberAsText = False
    .InconsistentFormula = False
    .OmittedCells = False
    .UnlockedFormulaCells = False
    .ListDataValidation = False
    .InconsistentTableFormula = False
End With

ActiveSheet.ClearArrows

仅正确设置检查空单元格的选项,但完全忽略“跟踪”功能的实际执行。有没有办法让这种情况自动发生,然后检查测试结果?

该功能是“公式选项卡”“错误检查”“引用空单元格的公式”“跟踪空单元格”

3 个答案:

答案 0 :(得分:1)

这是一个VBA方法,用于检查引用空单元格的公式。这不仅会告诉您它是哪个公式,还会告诉您哪个单元格为空。

为了测试这一点,在Sheet1中,Cell A1放置了这个公式

=F1+G1+H1

保持H1清空并填充F1和G1

在Cell A5中,输入此公式

=A1*D5

将Cell D5保持为空。

现在将此代码粘贴到模块中。

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim RngFormulas As Range, fCell As Range, _
    DPrcd As Range, aCell As Range

    Set ws = Sheets("Sheet1")

    With ws
        On Error Resume Next
        Set RngFormulas = .Cells.SpecialCells(xlCellTypeFormulas)
        On Error GoTo 0

        If Not RngFormulas Is Nothing Then
            For Each fCell In RngFormulas
                On Error Resume Next
                Set DPrcd = fCell.DirectPrecedents
                On Error GoTo 0
                If Not DPrcd Is Nothing Then
                    For Each aCell In DPrcd
                        If IsEmpty(aCell.Value) Then
                            Debug.Print aCell.Address & " in " & _
                            fCell.Formula & " is empty"
                        End If
                    Next
                End If
            Next
        End If
    End With
End Sub

运行宏时,您将在即时窗口中看到输出。

<强>快照

enter image description here

答案 1 :(得分:0)

试     = IF(COUNTA(A1:A3),“ok”,“error”)在数组中查找空格,如果通过,那么你可以做任何其他需要做的事情。

答案 2 :(得分:0)

我想运行公式&gt;错误检查我的大型电子表格的每一页,但我也找不到任何VBA代码来执行它。

我想出的最好的方法就是使用Goto&gt;选择错误。特殊,需要进行错误处理以忽略未发现错误时发生的错误(并且它对#N / A常量不起作用)。

Dim r As Range

On Error Resume Next
Set r = ActiveCell.SpecialCells(xlCellTypeFormulas, xlErrors)
If Err.Number <> 0 And Err.Number <> 1004 Then Stop
On Error GoTo 0
If Not r Is Nothing Then
    If r.Count > 0 Then
        r.Select
    End If
End If

如果错误除了1004“没有找到细胞”。发生这种情况,我没有比打破宏更能满足它。

相关问题