gpt4 book ai didi

excel - 用于检查公式的 VBA 查询

转载 作者:行者123 更新时间:2023-12-04 09:22:23 25 4
gpt4 key购买 nike

我正在尝试编写 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

最佳答案

来自 Range.HasFormula 文档:

True if all cells in the range contain formulas; False if none of the cells in the range contains a formula; null otherwise.


它的返回值由所有有或没有公式的单元格决定。如果只有一些有公式,那么它是 .
要解决您的问题,请在每个单独的单元格上使用循环:
Dim rng as Range
For Each rng in rr
If rng.HasFormula Then
rng.Interior.Color = vbRed
End If
Next
编辑 : 在你的循环尝试中,删除 Range称呼:
For Each cell in rr
编辑 2 : 你也可以使用 Range.SpecialCells :
On Error Resume Next '<~ an error will occur if there are no formula cells
Dim rng as Range
Set rng = rr.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0

If Not rng Is Nothing Then
rng.Interior.Color = vbRed
End If

关于excel - 用于检查公式的 VBA 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63077112/

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