Mike; Thanks for that post. We are re-inventing the wheel...
Rick..thanks for testing that....it is getting better & refined
Function GetCFColorIndex(C As Range) As Variant
Dim intCount As Integer, FC As FormatCondition, blnMatch As Boolean
If C.Count < 1 Then Exit Function
For intCount = 1 To C.FormatConditions.Count
'Loop through each Contidional Formatting
Set FC = C.FormatConditions(intCount)
If FC.Type = 1 Then
'Handle Type1-xlExpression (If 'Cell Value Is')
Select Case FC.Operator
Case xlBetween '1
If C.Value = GetCFV(FC.Formula1) And C.Value _
<= GetCFV(FC.Formula2) Then blnMatch = True: Exit For
Case xlNotBetween '2
If C.Value < GetCFV(FC.Formula1) Or C.Value _
GetCFV(FC.Formula2) Then blnMatch = True: Exit For
Case xlEqual '3
If C.Value = GetCFV(FC.Formula1) Then blnMatch = True: Exit For
Case xlNotEqual '4
If C.Value < GetCFV(FC.Formula1) Then blnMatch = True: Exit For
Case xlGreater '5
If C.Value GetCFV(FC.Formula1) Then blnMatch = True: Exit For
Case xlGreaterEqual '6
If C.Value = GetCFV(FC.Formula1) Then blnMatch = True: Exit For
Case xlLess '7
If C.Value < GetCFV(FC.Formula1) Then blnMatch = True: Exit For
Case xlLessEqual '8
If C.Value <= GetCFV(FC.Formula1) Then blnMatch = True: Exit For
End Select
Else
'Handle Type2-xlExternal (If 'Formula Is')
If Evaluate(Application.ConvertFormula( _
Application.ConvertFormula(FC.Formula1, xlA1, xlR1C1), _
xlR1C1, xlA1, , C)) Then blnMatch = True: Exit For
End If
Next
If blnMatch Then GetCFColorIndex = FC.Interior.ColorIndex
End Function
'-------------------------------------------------------------------------------
Function GetCFV(strData As Variant)
'Get text string or numeric from CF formula
If Not IsNumeric(strData) Then
GetCFV = Mid(strData, 3, Len(strData) - 3)
Else
GetCFV = CDbl(strData)
End If
End Function
'-------------------------------------------------------------------------------
If this post helps click Yes
---------------
Jacob Skaria
"Rick Rothstein" wrote:
Hey! We were having fun here and you (and Bob) ruined it for us.<g
I misread something earlier in the thread which seemed to indicate that
Bob's function wasn't complete, but in testing it, I see that it is. Thanks
for the wake-up call.
--
Rick (MVP - Excel)
"Mike H" wrote in message
...
Rick, Jacob,
Apologies for breaking in on your thread but I think Bob Philips has
already
done this
Bob's function or view his page
http://www.xldynamic.com/source/xld.CFConditions.html
'---------------------------------------------------------------------
Public Function CFColorindex(rng As Range)
'---------------------------------------------------------------------
Dim oFC As FormatCondition
Dim sF1 As String
Dim iRow As Long
Dim iColumn As Long
Set rng = rng(1, 1)
If rng.FormatConditions.Count 0 Then
For Each oFC In rng.FormatConditions
If oFC.Type = xlCellValue Then
Select Case oFC.Operator
Case xlEqual
CFColorindex = rng.Value = oFC.Formula1
Case xlNotEqual
CFColorindex = rng.Value < oFC.Formula1
Case xlGreater
CFColorindex = rng.Value oFC.Formula1
Case xlGreaterEqual
CFColorindex = rng.Value = oFC.Formula1
Case xlLess
CFColorindex = rng.Value < oFC.Formula1
Case xlLessEqual
CFColorindex = rng.Value <= oFC.Formula1
Case xlBetween
CFColorindex = (rng.Value = oFC.Formula1 And _
rng.Value <= oFC.Formula2)
Case xlNotBetween
CFColorindex = (rng.Value < oFC.Formula1 Or _
rng.Value oFC.Formula2)
End Select
Else
're-adjust the formula back to the formula that applies
'to the cell as relative formulae adjust to the activecell
With Application
iRow = rng.Row
iColumn = rng.Column
sF1 = .Substitute(oFC.Formula1, "ROW()", iRow)
sF1 = .Substitute(sF1, "COLUMN()", iColumn)
sF1 = .ConvertFormula(sF1, xlA1, xlR1C1)
sF1 = .ConvertFormula(sF1, xlR1C1, xlA1, , rng)
End With
CFColorindex = rng.Parent.Evaluate(sF1)
End If
If CFColorindex Then
If Not IsNull(oFC.Interior.ColorIndex) Then
CFColorindex = oFC.Interior.ColorIndex
Exit Function
End If
End If
Next oFC
End If 'rng.FormatConditions.Count 0
End Function
Mike
"Rick Rothstein" wrote:
Sorry, but I get both your old and new code missing some conditions (with
your new code missing more... fixing some of the ones the old code missed
plus adding new misses to the batch). Here are the set-ups that I get
your
code failing with...
Old Code
=================
Cell value is - not between 10 and 14 == Cell content = 1201
Cell value is - equal to ="" == Cell content is empty
Formula is - =SUM(A1,A3)=3 == Cell contents A1=2, A3=1
New Code
=================
Cell value is - equal to 2 == Cell content = 2
Cell value is - equal to ="Rick" == Cell content = Rick
Cell value is - greater than 0 == Cell content = 1
Cell value is - greater than or equal to 12 == Cell content = 12
Cell value is - between 10 and 14 == Cell content = 12
Formula is - =SUM(A1,A3)=3 == Cell contents A1=2, A3=1
--
Rick (MVP - Excel)
"Jacob Skaria" wrote in message
...
Rick, Just try any text string under the first type 'Cell Value' is
"A". I
have modified mine which passed the initial testing. It is time to have
a
look at XL07..
'UDF to get Conditional Formatting Color Index for a cell (XL 2003)
'-------------------------------------------------------------------------------
Function GetCFColorIndex(C As Range) As Variant
Dim intCount As Integer, FC As FormatCondition, blnMatch As Boolean
If C.Count < 1 Then Exit Function
For intCount = 1 To C.FormatConditions.Count
'Loop through each Contidional Formatting
Set FC = C.FormatConditions(intCount)
If FC.Type = 1 Then
'Handle Type1-xlExpression (If 'Cell Value Is')
Select Case FC.Operator
Case xlBetween '1
If C.Value = GetCFV(FC.Formula1) And C.Value _
<= GetCFV(FC.Formula2) Then blnMatch = True: Exit For
Case xlNotBetween '2
If C.Value < GetCFV(FC.Formula1) Or C.Value _
GetCFV(FC.Formula2) Then blnMatch = True: Exit For
Case xlEqual '3
If C.Value = GetCFV(FC.Formula1) Then blnMatch = True: Exit For
Case xlNotEqual '4
If C.Value < GetCFV(FC.Formula1) Then blnMatch = True: Exit For
Case xlGreater '5
If C.Value GetCFV(FC.Formula1) Then blnMatch = True: Exit For
Case xlGreaterEqual '6
If C.Value = GetCFV(FC.Formula1) Then blnMatch = True: Exit For
Case xlLess '7
If C.Value < GetCFV(FC.Formula1) Then blnMatch = True: Exit For
Case xlLessEqual '8
If C.Value <= GetCFV(FC.Formula1) Then blnMatch = True: Exit For
End Select
Else
'Handle Type2-xlExternal (If 'Formula Is')
If Evaluate(FC.Formula1) Then blnMatch = True: Exit For
End If
Next
If blnMatch Then GetCFColorIndex = FC.Interior.ColorIndex
End Function
'-------------------------------------------------------------------------------
Function GetCFV(strData As Variant)
'Get text string or numeric from CF formula
GetCFV = strData
If Not IsNumeric(strData) Then _
GetCFV = Mid(strData, 3, Len(strData) - 3)
End Function
'-------------------------------------------------------------------------------
If this post helps click Yes
---------------
Jacob Skaria
"Rick Rothstein" wrote:
I thought mine did... can you give me an example so I can hone in on
the
problem?
--
Rick (MVP - Excel)
"Jacob Skaria" wrote in
message
...
Rick; both of us have missed to handle text strings..
"Jacob Skaria" wrote:
Hi Rick
Yes; it is working and I really appreciate the enthusiasm and time
behind
this. However, dont you think it looks a bit complicated.
If you go by how Microsoft has designed the 'Conditional
Formatting'
user
interface and apply the same sequence while coding I think it is
quite
straightforward. The below can be used as a UDF for 2003 version.
Having
said
that I am not sure whether this would work for 2007.
Function GetCFColorIndex(C As Range) As Variant
Dim intCount As Integer, FC As FormatCondition, blnMatch As Boolean
If C.Count < 1 Then Exit Function
For intCount = 1 To C.FormatConditions.Count
'Loop through each Contidional Formatting
Set FC = C.FormatConditions(intCount)
If FC.Type = 1 Then
'Handle Type1-xlExpression (If 'Cell Value Is')
Select Case FC.Operator
Case xlBetween '1
If C.Value = FC.Formula1 And C.Value <= FC.Formula2 _
Then blnMatch = True: Exit For
Case xlNotBetween '2
If C.Value < FC.Formula1 Or C.Value FC.Formula2 Then _
blnMatch = True: Exit For
Case xlEqual '3
If C.Value = FC.Formula1 Then blnMatch = True: Exit For
Case xlNotEqual '4
If C.Value < FC.Formula1 Then blnMatch = True: Exit For
Case xlGreater '5
If C.Value FC.Formula1 Then blnMatch = True: Exit For
Case xlGreaterEqual '6
If C.Value = FC.Formula1 Then blnMatch = True: Exit For
Case xlLess '7
If C.Value < FC.Formula1 Then blnMatch = True: Exit For
Case xlLessEqual '8
If C.Value <= FC.Formula1 Then blnMatch = True: Exit For
End Select
Else
'Handle Type2-xlExternal (If 'Formula Is')
If Evaluate(FC.Formula1) Then blnMatch = True: Exit For
End If
Next
If blnMatch Then GetCFColorIndex = FC.Interior.ColorIndex
End Function
If this post helps click Yes
---------------
Jacob Skaria
"Rick Rothstein" wrote:
Ah, I see the problem now. Okay, what about the following
function
then? I
think I caught all the problem areas, but I can't be sure... the
Conditional
Format structure seems like such a complicated mess underneath it
all.
Function GetCellColorIndex(C As Range) As Variant
Dim X As Long, Op As Long, Condition As Boolean, FC As
FormatCondition
Dim CurrAddr As String, CVal As Variant, Operators() As String
Operators = Split("=,<,=,<,,<,=,<=,<=,", ",")
If C.Count = 1 Then
CurrAddr = ActiveCell.Address
C.Select
For X = 1 To C.FormatConditions.Count
Set FC = C.FormatConditions(X)
If FC.Type = xlExpression Then
If Evaluate(FC.Formula1) Then GoTo Done
Else
If IsEmpty(C.Value) Then
CVal = """"""
Else
CVal = C.Value
End If
Op = FC.Operator
If Op = xlBetween Then
If Evaluate(CVal & Operators(Op - 1) & FC.Formula1) And
_
Evaluate(CVal & Operators(Op + 7) & FC.Formula2) _
Then GoTo Done
ElseIf Op = xlNotBetween Then
If Evaluate(CVal & Operators(Op - 1) & FC.Formula1) Or
_
Evaluate(CVal & Operators(Op + 7) & FC.Formula2) _
Then GoTo Done
ElseIf Left(FC.Formula1, 1) = "=" Then
If Evaluate(CVal & Operators(Op - 1) & Mid(FC.Formula1,
2))
Then
GoTo Done
ElseIf Evaluate(CVal & Operators(Op - 1) & FC.Formula1)
Then
GoTo Done
End If
End If
Next