如何返回布尔值以有条件地格式化单元格?

时间:2022-02-24 07:51:07

The following code in VBE should return true, so that when invoked from the Conditional Format dialog the cell is modified accordingly:

VBE中的以下代码应返回true,以便在从“条件格式”对话框调用时,相应地修改单元格:

Function sshProblem()
   Dim portStatus As String
    portStatus = ActiveCell.Value
    If StrComp(portStatus, "No") = 0 Then
       sshProblem = True
    End If
End Function

If I apply this conditional settings to a cell with the value "No", nothing happens:

如果我将此条件设置应用于值为“No”的单元格,则没有任何反应:

如何返回布尔值以有条件地格式化单元格?

1 个解决方案

#1


3  

You need to pass the range into the function and not use active cell as that changes every time you change the selection:

您需要将范围传递给函数,而不是使用活动单元格,因为每次更改选择时都会更改:

Function sshProblem(rng As Range) As Boolean
   Dim portStatus As String
    portStatus = rng.Value
    If StrComp(portStatus, "No") = 0 Then
       sshProblem = True
    End If
End Function

Then you would call it using the upper left cell in the range over which you are formatting.

然后,您可以使用格式化范围内的左上角单元格来调用它。

So if I was formatting A1:A1000 the formula would be:

所以,如果我格式化A1:A1000公式将是:

=sshProblem(A1)

如何返回布尔值以有条件地格式化单元格?

But as @Mat'sMug, in his infinite wisdom, stated this can be done with a simple existing formula:

但正如@ Mat'sMug所说,用他的无限智慧说明这可以通过一个简单的现有公式来完成:

=A1="No"

No vba needed for something simple.

简单的东西不需要vba。

#1


3  

You need to pass the range into the function and not use active cell as that changes every time you change the selection:

您需要将范围传递给函数,而不是使用活动单元格,因为每次更改选择时都会更改:

Function sshProblem(rng As Range) As Boolean
   Dim portStatus As String
    portStatus = rng.Value
    If StrComp(portStatus, "No") = 0 Then
       sshProblem = True
    End If
End Function

Then you would call it using the upper left cell in the range over which you are formatting.

然后,您可以使用格式化范围内的左上角单元格来调用它。

So if I was formatting A1:A1000 the formula would be:

所以,如果我格式化A1:A1000公式将是:

=sshProblem(A1)

如何返回布尔值以有条件地格式化单元格?

But as @Mat'sMug, in his infinite wisdom, stated this can be done with a simple existing formula:

但正如@ Mat'sMug所说,用他的无限智慧说明这可以通过一个简单的现有公式来完成:

=A1="No"

No vba needed for something simple.

简单的东西不需要vba。