Detect if conditional format is true

  • Thread starter Thread starter Bill
  • Start date Start date
B

Bill

Hi people,

I have a worksheet with lots of different conditional
formats in different cells.

I want to be able to count all the red(or yellow etc)
cells on my worksheet.

However, this may mean cells that I have formatted
manually, which I know I can count using a test on cells
(x,y).interior.colorindex to increment a variable; or it
could mean cells that have been coloured in red by the
conditional format (using condition 1, 2 or 3, and a
different unrelated formula for each cell).

Hope you can help me,

Bill J. Duke
 
Bill,

I had the same problem exactly. I couldn't believe that
Excel would do all the work to conditionally format a
cell and then simply forget what it had done. There MUST
be a property called "CurrentFormat" or something, I
thought - until I found I wasn't alone.

Anyway, I decided to fix it, and here's the result. It
seems to work, but do please let me know if you find
anything wrong with it.

Sub detectRedCells()
' Detect cells with red interior colour put there by
conditional formatting
' Much quicker if sheet is protected and only cells of
interest are unlocked
' e.g. cells for data entry that may contain an error
indicated by a red interior
Dim c As Range
Dim r As Boolean
Dim a As String

' This assumes the cells of interest are unlocked
ActiveSheet.Protect

Set c = ActiveSheet.UsedRange.Cells(1, 1)
While Not c Is Nothing
r = False
If c.FormatConditions.Count <> 0 Then r =
checkCFormats(c)

' Here it compiles a list of red cells to display when
it's finished
If r Then a = a & c.Address & vbLf
If c.Next.Row <= c.Row And c.Next.Column <=
c.Column Then
Set c = Nothing
Else
Set c = c.Next
End If
Wend
MsgBox a

ActiveSheet.Unprotect

End Sub

Private Function checkCFormats(Cl As Range) As Boolean
' Return True if Cl is conditionally formatted with a
red interior
' Cl is known to have 1 or more FormatConditions
Dim n As Long

With Cl
For n = 1 To .FormatConditions.Count
If .FormatConditions(n).Interior.Color = vbRed
Then
If checkOneCF(Cl, .FormatConditions(n))
Then
checkCFormats = True
Exit Function
End If
End If
Next
End With

End Function

Private Function checkOneCF(Cl As Range, fCond As
FormatCondition) As Boolean

With fCond
If .Type = xlCellValue Then
checkOneCF = testCellValue(Cl.Value, fCond)
Else
checkOneCF = testFormula(fCond.Formula1)
End If
End With

End Function

Private Function testCellValue(Val As Variant, fCond As
FormatCondition) As Boolean
Dim v As Double
Dim f As Double

With fCond

v = CDbl(Val)
f = CDbl(.Formula1)
Select Case .Operator
Case xlEqual
testCellValue = (v = f)
Case xlNotEqual
testCellValue = (v <> f)
Case xlBetween
testCellValue = (v >= f) And (Val <= CDbl
(.Formula2))
Case xlNotBetween
testCellValue = (v < f) And (Val > CDbl
(.Formula2))
Case xlGreater
testCellValue = (v > f)
Case xlLess
testCellValue = (v < f)
Case xlGreaterEqual
testCellValue = (v >= f)
Case xlLessEqual
testCellValue = (v <= f)
End Select
End With

End Function

Private Function testFormula(Fm As String) As Boolean

On Error Resume Next
testFormula = Evaluate(Fm)

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top