How to highlight duplicate values with different colors
Tutor 5 (97 Reviews)
Excel Tutor
Still stuck with a Excel question
Ask this expertAnswer
Yes, you can highlight duplicate values with different colors in Excel using Conditional Formatting with custom formulas or by combining multiple formatting rules. Excel provides built-in duplicate highlighting features, but assigning unique colors to each set of duplicates requires specific techniques.
Using Conditional Formatting for Basic Duplicate Highlighting
To highlight all duplicates with a single color, navigate to the Home tab, select your data range, click Conditional Formatting, choose Highlight Cells Rules, and select Duplicate Values. This method applies one color to all duplicate entries but does not differentiate between different duplicate groups.
Highlighting Each Duplicate Group with a Unique Color Using Helper Columns
Step 1: Create a Helper Column
- Insert a new column next to your data
- Enter this formula in the first cell of the helper column:
=COUNTIF($A$1:A1,A1) - Drag the formula down to apply it to all rows
This formula counts how many times each value appears up to the current row, assigning sequential numbers to duplicates.
Step 2: Add Another Helper Column for Unique Identification
- Create a second helper column
- Enter this formula:
=IF(COUNTIF($A:$A,A1)>1,A1&"-"&B1,"") - Copy the formula down
This combines the original value with its occurrence number, creating unique identifiers for each duplicate instance.
Step 3: Apply Conditional Formatting with Multiple Rules
- Select your original data range
- Click Conditional Formatting, then New Rule
- Choose "Use a formula to determine which cells to format"
- Enter formulas targeting specific duplicate groups
- Assign different colors to each rule
- Repeat for each duplicate group you want to highlight
Using VBA to Automatically Assign Different Colors to Duplicate Groups
Step 1: Open VBA Editor
Press Alt + F11 (Windows) or Option + F11 (Mac) to access the Visual Basic Editor.
Step 2: Insert the VBA Code
- Click Insert, then Module
- Paste the following code:
Sub HighlightDuplicatesWithColors()
Dim rng As Range
Dim cell As Range
Dim dict As Object
Dim colorIndex As Long
Dim colors() As Long
colors = Array(65535, 52377, 13434828, 10284031, 16764057, 13408767, 10079487, 16777164)
Set dict = CreateObject("Scripting.Dictionary")
Set rng = Selection
colorIndex = 0
For Each cell In rng
If WorksheetFunction.CountIf(rng, cell.Value) > 1 Then
If Not dict.Exists(cell.Value) Then
dict.Add cell.Value, colors(colorIndex Mod 8)
colorIndex = colorIndex + 1
End If
cell.Interior.Color = dict(cell.Value)
End If
Next cell
End Sub
Step 3: Run the Macro
- Select your data range
- Press Alt + F8 (Windows) or Option + F8 (Mac)
- Choose HighlightDuplicatesWithColors
- Click Run
The macro assigns a unique color from the predefined array to each group of duplicate values.
Using COUNTIF with Multiple Conditional Formatting Rules
Step 1: Create the First Rule for First Duplicate Group
- Select your data range
- Go to Conditional Formatting, New Rule
- Select "Use a formula to determine which cells to format"
- Enter:
=COUNTIF($A:$A,$A1)>1 - Click Format, choose a color
- Click OK
Step 2: Add Specific Rules for Known Duplicate Values
- Create a new rule for each specific value
- Use formulas like:
=AND($A1="SpecificValue",COUNTIF($A:$A,$A1)>1) - Assign a different color to each rule
- Arrange rule priority in the Manage Rules dialog
Rules are evaluated from top to bottom, so place more specific rules above general ones.
Highlighting Duplicates Across Multiple Columns
To identify duplicates across multiple columns with different colors, concatenate column values in a helper column.
Creating the Helper Column
- In a new column, enter:
=A1&B1&C1 - Apply the formula to all rows
- Use the helper column as the basis for your conditional formatting rules
- Apply the methods described above to assign different colors
Using Power Query to Identify and Mark Duplicate Groups
Step 1: Load Data into Power Query
- Select your data range
- Go to Data tab, click From Table/Range
- Confirm the range in the dialog box
Step 2: Add Index Column for Duplicates
- In Power Query Editor, select your value column
- Click Add Column, then Index Column
- Sort by your value column
- Add a custom column with this formula:
Table.RowCount(Table.SelectRows(#"Previous Step", each [YourColumn] = [YourColumn]))
Step 3: Load and Format
- Click Close & Load
- Apply conditional formatting based on the duplicate group numbers
- Assign colors to different group numbers
Creating a Dynamic Color-Coded Duplicate Tracker
Using a Combination of Formulas and Conditional Formatting
- Create a helper column with:
=COUNTIF($A$1:$A1,$A1) - Create another column with:
=IF(COUNTIF($A:$A,$A1)>1,$A1,"") - Use this column to create multiple conditional formatting rules
- Each unique value gets its own rule with a specific color
Applying the Rules
- Select your data
- Create rules using formulas like:
=AND($A1=FirstDuplicateValue,COUNTIF($A:$A,$A1)>1) - Assign Color 1
- Repeat with:
=AND($A1=SecondDuplicateValue,COUNTIF($A:$A,$A1)>1) - Assign Color 2
- Continue for each duplicate group
Tips for Managing Multiple Color Rules
Organize your conditional formatting rules by accessing the Manage Rules dialog (Conditional Formatting > Manage Rules). Reorder rules by dragging them up or down to change priority. Delete outdated rules to maintain performance. Name your rules descriptively in newer Excel versions to track their purpose.
Limitations and Considerations
Excel's conditional formatting supports up to 64 rules per cell range in Excel 2007 and later versions. Applying numerous color rules to large datasets impacts workbook performance and calculation speed. The manual method of creating individual rules works best for datasets with a limited number of distinct duplicate groups. The VBA approach provides greater flexibility and automatically handles any number of duplicate groups with predefined colors.
. Was this Helpful?Get Online Tutoring or Questions answered by Experts.
You can post a question for a tutor or set up a tutoring session
Answers · 1
What is the formula to highlight duplicate values
Answers · 1
What is the formula to remove duplicates
Answers · 1
How to delete duplicate values in excel
Answers · 1
How to delete duplicate rows based on one column
Answers · 1