Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 用于检查公式的VBA查询_Excel_Vba - Fatal编程技术网

Excel 用于检查公式的VBA查询

Excel 用于检查公式的VBA查询,excel,vba,Excel,Vba,我正在尝试编写VBA代码,用于检查公式是否存在于一系列单元格中。下面是我的查询,不知怎么的,它不起作用(带有公式的单元格没有变成红色)。谁能帮帮我吗 Sub Test() Dim LResponse As Integer Set rr = Application.InputBox( _ prompt:="Select a range On this worksheet", _ Type:=8) If rr.HasForm

我正在尝试编写VBA代码,用于检查公式是否存在于一系列单元格中。下面是我的查询,不知怎么的,它不起作用(带有公式的单元格没有变成红色)。谁能帮帮我吗

Sub Test()
    Dim LResponse As Integer
    Set rr = Application.InputBox( _
        prompt:="Select a range On this worksheet", _
        Type:=8)
    If rr.HasFormula = TRUE Then
        rr.Interior.Color = vbRed
    End If
End Sub
编辑:我也试过循环

Sub Test()
    Set rr = Application.InputBox( _
        prompt:="Select a range On this worksheet", _
        Type:=8)
    For Each cell In Range(rr)
        If cell.HasFormula = TRUE Then
            cell.Interior.Color = vbRed
        End If
    Next
End Sub
从文档中:

如果范围内的所有单元格都包含公式,则为TrueFalse如果范围内的单元格均不包含公式null否则

其返回值由所有具有或不具有公式的单元格确定。如果只有一些公式,那么它是null

要解决问题,请在每个单元格上使用循环:

Dim rng as范围
对于rr中的每个rng
如果rng.has公式,则
rng.Interior.Color=vbRed
如果结束
下一个
编辑:在循环尝试中,放弃
范围
调用:

rr中每个单元格的

编辑2:您还可以使用:


On Error Resume Next'循环遍历范围内的每个单元格。或者对rr中的每个单元格使用
SpecialCells(xlCellTypeFormulas)
。仅供参考,如果启用了动态数组公式,则需要这样做:确保提取溢出结果。您这样做是为了查找(并检查)公式吗?如果是,你知道有一个显示公式的快捷方式吗?[在工作表中显示单元格值或公式之间切换。Ctrl+grave accent(`)]()