VBA函數測試cell是否在Excel中有條件地格式化

[英]VBA function to test if cell is conditionally formatted in Excel


I have written the function below to test if a cell has conditional formatting activated based upon the cell fill.

我編寫了下面的函數來測試一個單元格是否根據單元格填充激活了條件格式。

Function cfTest(inputCell)

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

It does not work however. Saying that, this method does.

然而,它不起作用。就是說,這個方法可以。

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

Can anyone explain to me why the function will not work?

有人能解釋一下為什么這個函數不能工作嗎?

Cheers.

歡呼。

EDIT: Updated function but not working for conditional formatting

編輯:更新函數,但不支持條件格式

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

4 个解决方案

#1


3  

Here is a working demo if the desired result. Column E looks at column D and displays the value TRUE if it is conditionally formatted by cell fill color. i.e. click on the name 'Bob', and conditionally formatting highlights the cell via the code below

如果需要的結果,這里有一個可用的演示。列E查看列D,如果值是按單元格填充顏色有條件地格式化的,則顯示為TRUE。例如,單擊“Bob”,並有條件地通過下面的代碼對單元格進行格式化

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

enter image description here

Click on another name, and the same result occurs.

單擊另一個名稱,就會發生相同的結果。

enter image description here

However, when I click off the names onto another cell, I last name selected remains highlighted, giving the impression of a button still depressed.

然而,當我在另一個單元格上單擊名字時,我選擇的姓仍然突出顯示,給人的印象是一個按鈕仍然處於抑郁狀態。

enter image description here

The VBA code behind is as follows.

后面的VBA代碼如下所示。

This sits within the Sheet1 code:

它位於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

And this is the method itself:

這就是方法本身

Sub cfTest()

Range("E:E").ClearContents

If ActiveCell.DisplayFormat.Interior.color <> 16777215 Then
    ActiveCell.Offset(0, 1) = True
End If

End Sub

The application I ended up building off this example had much more too it, but going back to the posted question, the cfTest() method allowed me to test if a cell was conditionally formatted based upon cell fill.

我最后構建這個示例的應用程序包含了更多的內容,但是回到剛才提到的問題,cfTest()方法允許我測試一個單元格是否根據單元格填充有條件地格式化。

#2


1  

Here are two related functions that implement mathematical conditions. This is slightly less complicated than the Chip Pearson version, and also less complete, but I think this should cover most cases, and this shouldn't be too difficult to extend.

下面是實現數學條件的兩個相關函數。這比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

This will work for some common operators, but there are two other operators (xlBetween and xlNotBetween) and there are other types of condition that would have to be caught as well, and the logic for some of those would be a little more complicated than this. Some of them, however (like databars), inherently convey that there is a condition, so no processing would be necessary.

這對於一些常見的操作符來說是可行的,但是還有另外兩個操作符(xlBetween和xlNotBetween),還有其他類型的條件也必須被捕獲,其中一些操作符的邏輯要比這個復雜一些。然而,其中一些(如數據庫)天生就表示存在一個條件,因此不需要進行任何處理。

Here is a link to the full documentation:

以下是完整文件的鏈接:

http://msdn.microsoft.com/en-us/ff835850(v=office.15)

http://msdn.microsoft.com/en-us/ff835850(v = office.15)

#3


1  

I'm not sure as to the why of this but maybe it'll help. VB doesn't seem to allow access to a cells color when that color is based on conditional formatting.

我不知道為什么會這樣,但也許會有幫助。當該顏色基於條件格式時,VB似乎不允許訪問單元格顏色。

For example..

例如. .

'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

That being said, is there a reason you couldn't just test the cell for whatever formatting rules you have in effect. That would eliminate the need for a UDF.

也就是說,您是否有理由不能僅僅測試單元格的格式規則。這將消除UDF的需要。

=IF(A1<50,False,True)

#4


0  

I would perform a prior check for the color index your condition is for using this:

我將對您所使用的顏色索引進行預先檢查:

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

Then your function

那么你的函數

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

Another solution to make things rock solid is to combine both function so that cfTest takes cfTest_color_chk as a parameter and cfTest_color_chk will return the value of the color to match...

另一個解決方案是合並這兩個函數,使cfTest以cfTest_color_chk為參數,而cfTest_color_chk將返回匹配顏色的值……

Hope this helps

希望這有助於

Pascal

帕斯卡


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/03/12/72077d9bc2df7fb45b58a6b9584b04c7.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com