4

在 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

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

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

4

3 回答 3

1

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

为了测试这一点,在 Sheet1 中,单元格 A1 输入了这个公式

=F1+G1+H1

保持 H1 为空并填充 F1 和 G1

在单元格 A5 中,输入此公式

=A1*D5

保持单元格 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

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

快照

在此处输入图像描述

于 2012-07-02T19:54:22.113 回答
0

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

于 2012-07-02T19:54:43.770 回答
0

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

我想出的最好方法是使用 Goto > Special 选择错误,这需要错误处理以忽略未发现错误时发生的错误(并且它不适用于 #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“未找到单元格”以外的错误。发生,我并没有比打破宏更能满足它。

于 2012-10-18T07:03:45.673 回答