gpt4 book ai didi

excel - 错误检查和跟踪空单元格

转载 作者:行者123 更新时间:2023-12-02 12:02:52 24 4
gpt4 key购买 nike

在 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

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

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

最佳答案

以下是检查引用空单元格的公式的 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

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

快照

enter image description here

关于excel - 错误检查和跟踪空单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11299620/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com