I think I reached a point that I need a fresh eyes to look at the code as I can`t figure out why the code does what it does
... here is what I`ve done so far ..
1- the code applies three formulas of Conditional Formatting . ( check)
2- then look for cells with that conditional Format applied to .. and convert the FormatConditions.Interior.Color into cell.Interior.Color . (check)
3- Delete the Conitional Formatting from the cells (check)
4- look for cells with "red" color and put them in the column AA ( start of the problem here I guess )
5- look for cells with "Yellow" color and put them in AC .( the code doesn`t do this step at all)
6- look for cells with "green" color and put them in AE
7- delete duplicates from column AA .
8- delete Duplicates from Column AC
9- delete Duplicated from column AE
I attached the sheet with the macro in it .. hopefully someone will be able to spot what is wrong with the code .. thank you in advance
here is the code as well
Function ConditionalColor(rg As Range, FormatType As String) As Long
'Returns the color index (either font or interior) of the first cell in range rg. If no _
conditional format conditions apply, Then returns the regular color of the cell. _
FormatType Is either "Font" Or "Interior"
Dim cel As Range
Dim tmp As Variant
Dim boo As Boolean
Dim frmla As String, frmlaR1C1 As String, frmlaA1 As String
Dim i As Long
'Application.Volatile 'This statement required if Conditional Formatting for rg is determined by the _
value of other cells
Set cel = rg.Cells(1, 1)
Select Case Left(LCase(FormatType), 1)
Case "f" 'Font color
ConditionalColor = cel.Font.ColorIndex
Case Else 'Interior or highlight color
ConditionalColor = cel.Interior.ColorIndex
End Select
If cel.FormatConditions.Count > 0 Then
'On Error Resume Next
With cel.FormatConditions
For i = 1 To .Count 'Loop through the three possible format conditions for each cell
frmla = .Item(i).Formula1
If Left(frmla, 1) = "=" Then 'If "Formula Is", then evaluate if it is True
'Conditional Formatting is interpreted relative to the active cell. _
This cause the wrong results If the formula isn 't restated relative to the cell containing the _
Conditional Formatting--hence the workaround using ConvertFormula twice In a row. _
If the Function were Not called using a worksheet formula, you could just activate the cell instead.
frmlaR1C1 = Application.ConvertFormula(frmla, xlA1, xlR1C1, , ActiveCell)
frmlaA1 = Application.ConvertFormula(frmlaR1C1, xlR1C1, xlA1, xlAbsolute, cel)
boo = Application.Evaluate(frmlaA1)
Else 'If "Value Is", then identify the type of comparison operator and build comparison formula
Select Case .Item(i).Operator
Case xlEqual ' = x
frmla = cel & "=" & .Item(i).Formula1
Case xlNotEqual ' <> x
frmla = cel & "<>" & .Item(i).Formula1
Case xlBetween 'x <= cel <= y
frmla = "AND(" & .Item(i).Formula1 & "<=" & cel & "," & cel & "<=" & .Item(i).Formula2 & ")"
Case xlNotBetween 'x > cel or cel > y
frmla = "OR(" & .Item(i).Formula1 & ">" & cel & "," & cel & ">" & .Item(i).Formula2 & ")"
Case xlLess ' < x
frmla = cel & "<" & .Item(i).Formula1
Case xlLessEqual ' <= x
frmla = cel & "<=" & .Item(i).Formula1
Case xlGreater ' > x
frmla = cel & ">" & .Item(i).Formula1
Case xlGreaterEqual ' >= x
frmla = cel & ">=" & .Item(i).Formula1
End Select
boo = Application.Evaluate(frmla) 'Evaluate the "Value Is" comparison formula
End If
If boo Then 'If this Format Condition is satisfied
On Error Resume Next
Select Case Left(LCase(FormatType), 1)
Case "f" 'Font color
tmp = .Item(i).Font.ColorIndex
Case Else 'Interior or highlight color
tmp = .Item(i).Interior.ColorIndex
End Select
If Err = 0 Then ConditionalColor = tmp
Err.Clear
On Error GoTo 0
Exit For 'Since Format Condition is satisfied, exit the inner loop
End If
Next i
End With
End If
End Function
Sub ConditionalFormatting()
'
' ConditionalFormatting Macro
'
Dim g As Integer
Dim h As Integer
Dim u As Integer
Dim s As String
g = 5
h = 5
u = 5
Range("C6:Y611").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($C$6:$Y$611,C6)=4"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColor = 255
.Color = 255
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($C$6:$Y$611,C6)=3"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=COUNTIF($C$6:$Y$611,C6)=2"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 5287936
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = True
' ----------------------------------------------------
Dim cel As Range
Application.ScreenUpdating = False
'Remove conditional formatting from entire worksheet
'For Each cel In ActiveSheet.UsedRange.SpecialCells(xlCellTypeAllFormatConditions)
For Each cel In Selection 'Remove conditional formatting from selected cells
If cel.FormatConditions.Count > 0 Then
cel.Interior.ColorIndex = ConditionalColor(cel, "Interior") 'Replace the interior (highlight) color
cel.Font.ColorIndex = ConditionalColor(cel, "Font") 'Replace the font color
End If
Next cel
Selection.FormatConditions.Delete 'Delete all the Format Conditions for this cell
For Each Cell In Selection
If Cell.Interior.Color = 255 Then
Range("AA1").Offset(g, 0).Value = Cell.Value
g = g + 1
End If
If Cell.Interior.Color = 65535 Then
Range("AC1").Offset(h, 0).Value = Cell.Value
h = h + 1
End If
If Cell.Interior.Color = 5287936 Then
Range("AE1").Offset(u, 0).Value = Cell.Value
u = u + 1
End If
Next
Dim x As Long
Dim LastRow As Long
LastRow = Range("AA65536").End(xlUp).Row
For x = LastRow To 1 Step -1
If Application.WorksheetFunction.CountIf(Range("AA1:AA" & x), Range("AA" & x).Text) > 1 Then
Range("AA" & x).Delete
End If
Next x
LastRow = Range("AC65536").End(xlUp).Row
For x = LastRow To 1 Step -1
If Application.WorksheetFunction.CountIf(Range("AC1:AC" & x), Range("AC" & x).Text) > 1 Then
Range("AC" & x).Delete
End If
Next x
LastRow = Range("AE65536").End(xlUp).Row
For x = LastRow To 1 Step -1
If Application.WorksheetFunction.CountIf(Range("AE1:AE" & x), Range("AE" & x).Text) > 1 Then
Range("AE" & x).Delete
End If
Next x
Application.ScreenUpdating = True
End Sub
Bookmarks