VB Conditional formatting for three matches or no matches

sachavez

Active Member
Joined
May 22, 2009
Messages
469
Excel 2007

I am using the following code to find duplicates and change the font to red. The code works well.

Code:
    NUMROWS = ActiveSheet.UsedRange.Rows.Count
    Range("E2:E" & NUMROWS).Select
    Set uv = Selection.FormatConditions.AddUniqueValues
    uv.DupeUnique = xlDuplicate
    uv.Font.Color = vbRed

Next, I would like to change the cell color to vbYellow (cell color in column E) if there are three of the same number in column I.

And then, change the cell color to vbGreen if there are no matches.

Any advice?

Thanks, in advance.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Code:
Sub test()

NUMROWS = ActiveSheet.UsedRange.Rows.Count
Range("E2:E" & NUMROWS).Select
Set uv = Selection.FormatConditions.AddUniqueValues
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
uv.DupeUnique = xlDuplicate
uv.Font.Color = vbRed

Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=AND(COUNTIF(E:E, ""=""&E2)>=3,E2<>"""")"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).Interior.Color = vbYellow

Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=COUNTIF($E$2:$E$10, E2)=1"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).Interior.Color = vbGreen
Selection.FormatConditions(1).StopIfTrue = False

End Sub
 
Upvote 0
Code Ninja,

I apprecaite your blackbelt skills! This code looks promising. I will run on Saturday AM and give feedback!

Thanks a million!

Steve
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,183
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top