I have a formula to conditionally format text in a column based on the value from another cell (not in same column):
=SUM(COUNTIF(A1,"*" & $B$1:$E$1 & "*")) = 1
More detailed description can be found here.
I ran into problem while trying to apply this formula with the help of a macro I've recorded with "Record a macro" in Excel 2010.
Here is the process I do:
1) First I select the entire column (by clicking on column letter on top).
2) Then I go to "Conditional Formatting > New rule > Use a formula to determine which cells to format", enter my formula and choose a fill color for my formatting.
3) I then repeat the step 2 for 3 other conditional formattings with different formulas as well as different fill colors:
=SUM(COUNTIF(A1,"*" & $B$1:$E$1 & "*")) = 2
=SUM(COUNTIF(A1,"*" & $B$1:$E$1 & "*")) = 3
=SUM(COUNTIF(A1,"*" & $B$1:$E$1 & "*")) = 4
So here is the problem: while recording the macro I get the desired formatting, i.e. all the cells in A column are color coded depending on how many values from cells B1, C1, D1 and E1 match it's contents.
However when I try to run previously recorded macro on unformated column it doesn't work as intended, only changing color of some cells and only in one color.
So the question is: How do I apply multiple conditional formatting to the same column with help of macro in Excel 2010?
Below is a full macro code for your references:
Sub Macro6()
'
' Macro6 Macro
'
'
Columns("A:A").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=SUM(COUNTIF(A1,""*"" & $B$1:$E$1 & ""*"")) = 1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=SUM(COUNTIF(A1,""*"" & $B$1:$E$1 & ""*"")) = 2"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=SUM(COUNTIF(A1,""*"" & $B$1:$E$1 & ""*"")) = 3"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 15773696
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=SUM(COUNTIF(A1,""*"" & $B$1:$E$1 & ""*"")) = 4"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub