Highlight duplicates in a filtered Excel Table
The image above demonstrates a conditional formatting formula applied to an Excel Table containing random data. The Excel Table has been filtered to show only records for January 2012.
There is a built-in function in Excel that lets you highlight duplicates, however, that won't work properly if you have filtered the data.
The image above shows the built-in tool highlighting duplicate items. But one item exists only once in the filtered data, however, it is highlighted as a duplicate.
The reason is that there is another item not showing in the filtered table. You need to rely on a custom conditional formatting formula if you want it to compare only filtered visible values.
Build an Excel Table
The reason I am using an Excel Table is that the conditional formatting adjusts automatically if you add or delete records. It also uses structured references so there is no need to change cell references in the formula.
The disadvantage is that you have to use the INDIRECT function each time you reference the Excel Table in the CF formula, it is not a big deal but it is worth noting.
- Select any cell in your data set.
- Press short cut keys CTRL + T to open the "Create Table" dialog box.
- Enable the checkbox if the columns in the data set have header names.
- Press with left mouse button on OK button to apply.
Create a new conditional formatting rule
- Select table column "Description".
- Go to the "Home" tab on the ribbon.
- Press with left mouse button on the "Conditional formatting" button.
- Press with left mouse button on "New Rule..".
- Press with left mouse button on "Use a formula to determine which cells to format".
- Copy this conditional formatting formula:
=SUM(COUNTIF(INDIRECT("Table2[@Description]"), IF(SUBTOTAL(3, OFFSET(INDIRECT("Table2[Description]"), MATCH(ROW(INDIRECT("Table2[Description]")), ROW(INDIRECT("Table2[Description]")))-1, 0, 1)), INDIRECT("Table2[Description]"), "")))>1
- Paste to "Format values where this formula is true:". I will explain the formula later in this article.
- Press with left mouse button on the "Format..." button.
- Go to tab "Fill".
- Pick a color.
- Press with left mouse button on OK button.
- Press with left mouse button on OK button again.
Explaining CF formula in cell C211
This Conditional Formatting formula highlights cells that have a duplicate in a filtered Excel Table, note that it will not be highlighted if a duplicate exists outside the filtered values (not visible).
Step 1 - Create an array from 1 to n
The INDIRECT function makes it possible to use a reference to an Excel Table inside a Conditional Formatting formula. This article explains it in greater detail: How to use an Excel Table name in Data Validation Lists and Conditional Formatting formulas
The ROW function converts a cell reference to the corresponding row numbers which is useful when you want to create an array.
The MATCH function is utilized in this example to create an array that contains a sequence that begins with 1 and increments with one up to the number of values in the array.
MATCH(ROW(INDIRECT("Table2[Description]")), ROW(INDIRECT("Table2[Description]"))
becomes
MATCH(ROW(C206:C227), ROW(C206:C227))
becomes
MATCH({3; 4; ... 383; 384}, {3; 4; ... 383; 384})
and returns
{1; 2; 3; ... 380; 381}.
The Excel Table has 381 records and the size of the array matches that number.
Step 2 - Modify array
This workaround makes it possible to use an array of values in a SUBTOTAL function, the OFFSET function splits the array into smaller arrays with only one value in each array.
OFFSET(reference,rows,columns,[height],[width])
It returns error values, however, the SUBTOTAL function can calculate these values anyway.
OFFSET(INDIRECT("Table2[Description]"), MATCH(ROW(INDIRECT("Table2[Description]")), ROW(INDIRECT("Table2[Description]")))-1, 0, 1)
becomes
OFFSET(INDIRECT("Table2[Description]"), {1; 2; 3; ... 380; 381}-1, 0, 1)
becomes
{0; 1; 2; ... ; 379; 380}
and returns
{#VALUE!; #VALUE!; #VALUE!; ... ; #VALUE!; #VALUE!}
Step 3 - Identify visible values
The first argument in the SUBTOTAL function is 3 and represents the COUNTA function meaning it will count cells that are not empty.
The SUBTOTAL function will actually return an array with this setup which is very handy in this situation.
SUBTOTAL(3, OFFSET(INDIRECT("Table2[Description]"), MATCH(ROW(INDIRECT("Table2[Description]")), ROW(INDIRECT("Table2[Description]")))-1, 0, 1))
becomes
SUBTOTAL(3, {#VALUE!; #VALUE!; #VALUE!; ... ; #VALUE!; #VALUE!})
and returns {0; 0; 0; ... ; 0; 0}. 0 (zero) represents a hidden value and 1 is visible.
Step 4 - Create an array containing visible values
The IF function replaces 0 (zero) with nothing "" and 1 with the actual corresponding value.
IF(SUBTOTAL(3, OFFSET(INDIRECT("Table2[Description]"), MATCH(ROW(INDIRECT("Table2[Description]")), ROW(INDIRECT("Table2[Description]")))-1, 0, 1)), INDIRECT("Table2[Description]"), "")
becomes
IF({0; 0; 0; ... ; 0; 0}, INDIRECT("Table2[Description]"), "")
and returns {""; ""; ""; ... ; ""; ""}.
Step 5 - Check the number of times the current value exists across visible values
The COUNTIF function will return an array with this setup which we then can use to calculate a total. The reason we don't change the arguments with each other is that the range argument will not accept an array based on calculations.
COUNTIF(range, criteria)
COUNTIF(INDIRECT("Table2[@Description]"), IF(SUBTOTAL(3, OFFSET(INDIRECT("Table2[Description]"), MATCH(ROW(INDIRECT("Table2[Description]")), ROW(INDIRECT("Table2[Description]")))-1, 0, 1)), INDIRECT("Table2[Description]"), ""))
becomes
COUNTIF(INDIRECT("Table2[@Description]"), {""; ""; ""; ... ; ""; ""})
becomes
COUNTIF("Chez Quiz", {""; ""; ""; ... ; ""; ""})
and returns {0; 0; 0; ... ; 0; 0}. The entire array is not shown, however, it contains a few 1's as well.
Step 6 - Calculate a total
The SUM function adds all numbers in the array and returns a total.
SUM(COUNTIF(INDIRECT("Table2[@Description]"), IF(SUBTOTAL(3, OFFSET(INDIRECT("Table2[Description]"), MATCH(ROW(INDIRECT("Table2[Description]")), ROW(INDIRECT("Table2[Description]")))-1, 0, 1)), INDIRECT("Table2[Description]"), "")))
becomes
SUM({0; 0; 0; ... ; 0; 0})
and returns 2.
Step 7 - Check if value is larger than 1
The > larger than character is a logical operator that lets you compare values, it will return a boolean value TRUE or FALSE based on the outcome. The Conditional Formatting formula uses the boolean function to determine if the cell values should be highlighted or not.
SUM(COUNTIF(INDIRECT("Table2[@Description]"), IF(SUBTOTAL(3, OFFSET(INDIRECT("Table2[Description]"), MATCH(ROW(INDIRECT("Table2[Description]")), ROW(INDIRECT("Table2[Description]")))-1, 0, 1)), INDIRECT("Table2[Description]"), "")))>1
becomes
2>1
and returns True. Cell C211 is highlighted.
This article shows you how to easily identify duplicate rows or records in a list. What's on this webpage Conditional […]
The image above demonstrates a conditional formatting formula that highlights duplicate items based on date. The first instance is not highlighted, […]
The following conditional formula highlights only the second instance or more of a value in a cell range. Conditional formatting […]
Table of Contents How to highlight duplicate values Highlight the smallest duplicate number 1. How to highlight duplicate values The […]
Question: My scenario is tracking employees who complete online training. I capture their name, id, class taken, and date. Any […]
This article describes how to highlight duplicate records arranged into a column each, if you are looking for records entered […]
Question: How do I highlight the smallest duplicate value in a column using conditional formatting? Answer: Conditional formatting formula in […]
Question: I need to delete duplicates in two different columns together. How do I highlight the second or more duplicates […]
The image above demonstrates a conditional formatting formula that colors a record if there is at least one record that […]
This article explains how to count cells highlighted with Conditional Formatting (CF). The image above shows data in cell range […]
adam asks: Hi, I have a situation where I want to count if this value is duplicate and if it […]
Question: How do I highlight dates that meet criteria using conditional formatting? Table of contents Highlight values in a column […]
This post demonstrates how to highlight records with the closest value to a criterion, you can also choose to highlight […]
The image above shows rows highlighted based on value in column C being the largest or smallest in that particular […]
This article shows you how to easily identify duplicate rows or records in a list. What's on this webpage Conditional […]
Here is how to highlight every other row using conditional formatting. Conditional formatting formula: =ISEVEN(ROW())*OR($B3:$D3<>"") Alternative CF formula: =EVEN(ROW())=ROW() This […]
Question: I have a list that I keep adding rows to. How do i create a border that expands as […]
The image above shows you how to highlight rows with multiple criteria using OR logic. The criteria are found in […]
In this article, I will demonstrate how to search a table using conditional formatting. The criteria highlight matching column and […]
Today I am going to show you how to quickly compare two tables using Conditional Formatting (CF). I am going […]
The image above demonstrates a conditional formatting formula that highlights duplicate items based on date. The first instance is not highlighted, […]
A conditional formatting formula highlights values in column B that also exist in column D. =COUNTIF($D$3:$D$7,B3) The same thing happens […]
In this post I am going to try to explain formula basics in conditional formatting. It is really good if […]
The image above shows conditional formatting highlighting unique distinct values, duplicates are not highlighted. Conditional Formatting Formula: =COUNTIF($B$3:B3, B3)=1 The […]
This article demonstrates how to apply different cell formatting to a cell range based on a Drop Down list, column […]
In this blog post I will demonstrate a conditional formatting formula that will highlight common records in two lists. The […]
The image above demonstrates a conditional formatting formula that highlights records that only exist in one table. There are two […]
This article demonstrates different ways to reference an Excel defined Table in a drop-down list and Conditional Formatting. There are […]
This article demonstrates two formulas that extract distinct values from a filtered Excel Table, one formula for Excel 365 subscribers […]
This article demonstrates a formula that counts unique distinct values filtered from an Excel defined Table. Debra Dalgleish described in […]
This article demonstrates how to populate a drop down list with filtered values from an Excel defined Table. The animated […]
This article demonstrates how to filter records occurring in only one out of two Excel defined tables. It also shows […]
This article describes a macro that hides specific columns automatically based on values in two given cells. I am also […]
The image above demonstrates a macro linked to a button. Press with left mouse button on the button and the […]
This article demonstrates how to filter duplicate records using a simple formula and an Excel defined table.
I will in this article demonstrate a macro that copies criteria from one Excel Table and applies them to another […]
In this tutorial, I am going to demonstrate how to filter an Excel define Table through a VBA macro. How it […]
This article explains how to filter a data set based on extremely many conditions in an Excel defined Table, in […]
This blog post demonstrates how to filter unique distinct values from an Excel table dynamically. When you change or add […]
Today I am going to demonstrate how amazing pivot tables are! Take a look at this time sheet. You can […]
I will in this article demonstrate three different techniques to sort a data set in Excel. The first method sorts […]
This article demonstrates how to insert and use a scroll bar (Form Control) in Excel. It allows the user to […]
An Excel table allows you to easily sort, filter and sum values in a data set where values are related.
This article demonstrates a macro that automatically applies a filter to an Excel defined Table based on the result from […]
The filter feature in Excel won't allow you to do OR logic between columns, however, you can if you allow […]
This article explains how to calculate the largest and smallest number based on a condition which is if the number […]
6 Responses to “Highlight duplicates in a filtered Excel Table”
Leave a Reply to Gene Haines
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Thank you for the great post! I had spent almost a full day searching for how to highlight duplicates in a filtered list. But I have one further question. I am filtering this based on duplicate dollar amounts. How could I add in a part of the function to only highlight the duplicate amounts over $900 in the filtered table?
Oscar, Our company requires all employees to take 30 Mandatory Annual Courses. I have been tasked with streamlining the class schedule. I created in access a query that pulls all courses and personnel that are due to take these courses within 45 days. I export to excel and then sort on the courses and then the supervisor. After that I subtotal and get 30 subtotals for each course. The number of records in each subtotal course varies.
My max class size per course is 14 and the max number of people per supervisor is 5. How can I
Oscar,
My company requires all employees to take Mandatory Annual Courses.
We have 30 courses.
I have been tasked with streamlining the class schedule. I have all courses listed along with the names of the personnel along with their department and supervisor. I sort by class name and then by supervisor. I then subtotal and get 30 separate subtotals per course. Number of records in each subtotal will vary.
My max class size is 14 and the most I can pull from each supervisor per class per course is 5.
How can I create a formula for each subtotal that will determine the number of classes based on a max class size of 14 and then assign the max number of personnel per class per supervisor of 5.
Example: Course Name: Active Shooter
42 people need to attend this class, There are 6 supervisors for these 42 people. On the surface it looks like 3 classes,however one supervisor has 16 people which would exceed the max per class of 5.
Any help is appreciated.
Regards
Gene Haines
Gene Haines,
My max class size is 14 and the most I can pull from each supervisor per class per course is 5. 42 people need to attend this class, there are 6 supervisors for these 42 people.
You only have 6 supervisors, don't you need more supervisors per class per course if your class is 42? If each supervisor has 5 people you get 6x5 = 30 people.
This criteria is not met if your class is above 30 people?
exactly what i was searching for. unfortunately way more cumbersome than i hoped, but a solution nevertheless. thanks a lot!
Feliz tarde, el archivo de trabajo ya no está disponible. Podría volver a colocarlo??