VBA函数来testing单元格是否在Excel中有条件格式化

我写了下面的函数来testing单元格是否具有基于单元格填充激活的条件格式化。

Function cfTest(inputCell) If inputCell.DisplayFormat.Interior.Color <> 16777215 Then cfTest = True Else cfTest = False End If End Function 

但它不起作用。 说这个方法呢。

 Sub myCFtest() Dim R As Integer R = 2 Do If Range("I" & R).DisplayFormat.Interior.Color <> 16777215 Then Range("K" & R).Value = True Else Range("K" & R).Value = False End If R = R + 1 Loop Until R = 20 End Sub 

任何人都可以向我解释为什么该function无法正常工作?

干杯。

编辑 :更新的function,但不适用于条件格式

 Function cfTest(inputCell) If inputCell.Interior.ColorIndex <> -4142 Then cfTest = True Else cfTest = False End If End Function 

如果想要的结果,这是一个工作演示。 列E查看D列,如果通过单元格填充颜色有条件地格式化,则显示TRUE值。 即点击名字“鲍勃”,有条件格式化通过下面的代码突出显示单元格

 =IF(AND(CELL("row")=ROW(D1),CELL("col")=COLUMN(D1)),TRUE) 

在这里输入图像说明

点击另一个名字,并出现相同的结果。

在这里输入图像描述

但是,当我点击名称到另一个单元格时,我select的姓氏仍然突出显示,给人的印象仍然郁闷。

在这里输入图像说明

后面的VBA代码如下。

这位于Sheet1代码中:

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Target.Column = 4 And Target.Row <= Application.WorksheetFunction.CountA(Range("D:D")) Then Range("D:D").Calculate Call cfTest End If End Sub 

这是方法本身:

 Sub cfTest() Range("E:E").ClearContents If ActiveCell.DisplayFormat.Interior.color <> 16777215 Then ActiveCell.Offset(0, 1) = True End If End Sub 

我最终构build这个例子的应用程序还有更多,但回到发布的问题,cfTest()方法允许我testing一个单元格是否有条件格式化基于单元格填充。

这里有两个相关的函数来实现math条件。 这比Chip Pearson版稍微简单一点,也不太完整,但我认为这应该覆盖大多数情况,这不应太难以延伸。

 Function isConditionallyFormatted(rng As Range) As Boolean Dim f As FormatCondition On Error Resume Next isConditionallyFormatted = False For Each f In rng.FormatConditions isConditionallyFormatted = checkFormula(rng.Value, f.operator, f.Formula1) isConditionallyFormatted = checkFormula(rng.Value, f.operator, f.Formula2) Next End Function Function checkFormula(rng As Variant, operator As Variant, condition As Variant) On Error GoTo errHandler: Dim formula As String condition = Right(condition, Len(condition) - 1) Select Case operator Case xlEqual: formula = rng & "=" & condition Case xlGreater: formula = rng & ">" & condition Case xlGreaterEqual: formula = rng & ">=" & condition Case xlLess: formula = rng & "<" & condition Case xlLessEqual: formula = rng & "<=" & condition Case xlExpression: formula = condition End Select checkFormula = Evaluate(formula) Exit Function errHandler: Debug.Print Err.Number & " : " & Err.Description End Function 

这将适用于一些常见的操作符,但还有两个其他操作符(xlBetween和xlNotBetween),还有其他types的条件也必须被捕获,其中一些的逻辑会比这个复杂一点。 然而,其中一些(如数据库)固有地表示有条件,所以不需要处理。

以下是完整文档的链接:

http://msdn.microsoft.com/en-us/ff835850(v=office.15&#xFF09;

我不确定这是为什么,但也许会有所帮助。 当这种颜色基于条件格式时,VB似乎不允许访问单元格的颜色。

例如..

 'cell A1 colored yellow through conditional formatting MsgBox Range("A1").Interior.ColorIndex 'returns the incorrect result of -4142 regardless of cell color 'cell B1 colored yellow via the fill option on the ribbon MsgBox Range("B1").Interior.ColorIndex 'returns the correct result of 6 

话虽如此,是否有一个原因,你不能只是testing单元格,你有什么格式的规则生效。 这将消除对UDF的需要。

 =IF(A1<50,False,True) 

我会执行一个事先检查您的条件是使用此颜色索引:

 Function cfTest_color_chk(inputCell As Range) cfTest_color_chk = inputCell.Interior.ColorIndex End Function 

然后你的function

 Function cfTest(inputCell As Range) If inputCell.Interior.ColorIndex <> -4142 Then cfTest = True Else cfTest = False End If End Function 

另一个解决scheme是将两个函数结合起来,以便cfTest将cfTest_color_chk作为参数,而cfTest_color_chk将返回颜色的值以匹配…

希望这可以帮助

帕斯卡尔