Formulas to count duplicate rows on sheet

Deutz

Board Regular
Joined
Nov 30, 2009
Messages
191
Office Version
  1. 365
Platform
  1. Windows
Hi and thanks in advance,

I'm wondering how to build a couple of formulas that don't use a helper column/concat column. One formula should count duplicate rows (on a single sheet) including the first occurrence of the duplicate. The second formula should count duplicate rows (on a single sheet) and NOT include the first occurrence of the duplicate. The sheet can have multiple columns.

Thanks
Deutz
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
You will find what your are looking for with COUNTIFS. I don't know if you want to sum all duplicates, or have in from of each row the number of duplicates for this row?
 
Upvote 0
Try to post a small sample along with the desired results.

Sorry, I should have made it clearer.

With the following set of data the first formula should count all duplicate rows including the first occurrence. Rows to count are shown in red here, so the count = 5

Column A
Column B
Column C
1
aaa
xxx
2
bbb
rrr
1
aaa
xxx
3
aaa
xxx
1
aaa
xxx
5
ccc
qqq
2
bbb
rrr
1
aaa
zzz

<tbody>
</tbody>
















With the following set of data the second formula should count duplicate rows but NOT including the first occurrence. Rows to count are shown in red here, so the count = 3

Column A
Column B
Column C
1
aaa
xxx
2
bbb
rrr
1
aaa
xxx
3
aaa
xxx
1
aaa
xxx
5
ccc
qqq
2
bbb
rrr
1
aaa
zzz

<tbody>
</tbody>
















The number of colums may vary over time.

Thanks
Deutz
 
Upvote 0
For the first solution, I propose : {=SUM(IF(COUNTIFS($A$1:$A$10,A1:A10,$B$1:$B$10,B1:B10,$C$1:$C$10,C1:C10)>1,1,0))}. You will need to press ctrl+shift+enter as this is an array formula.

For the second, this one seems to work : =SUM(IF(COUNTIFS($A$1:$A$10,A1:A10,$B$1:$B$10,B1:B10)>1,(COUNTIFS($A$1:$A$10,A1:A10,$B$1:$B$10,B1:B10)-1)/COUNTIFS($A$1:$A$10,A1:A10,$B$1:$B$10,B1:B10),0)). Basically we do the same but instead of summing 1, we sum (x-1)/x where x is the number of occurrence.
 
Last edited:
Upvote 0
For the first solution, I propose : {=SUM(IF(COUNTIFS($A$1:$A$10,A1:A10,$B$1:$B$10,B1:B10,$C$1:$C$10,C1:C10)>1,1,0))}. You will need to press ctrl+shift+enter as this is an array formula.

For the second, this one seems to work : =SUM(IF(COUNTIFS($A$1:$A$10,A1:A10,$B$1:$B$10,B1:B10)>1,(COUNTIFS($A$1:$A$10,A1:A10,$B$1:$B$10,B1:B10)-1)/COUNTIFS($A$1:$A$10,A1:A10,$B$1:$B$10,B1:B10),0)). Basically we do the same but instead of summing 1, we sum (x-1)/x where x is the number of occurrence.

Thanks so much for that, NotoriousPopol and for the explanation. Works fine for the examples I provided. Just one other wrinkle ... is it possible to make this work in the case where some of the dupe rows have no values in one or more columns?

Also wondering about the range references in the COUNTIFS function as to why the first reference is absolute and the second is relative?

Thanks
Deutz
 
Last edited:
Upvote 0
Hi,

Do you want rows to match when there is empty data? For example, should this configuration match :

- 1 / aaa / xxx and 1 / / xxx ?
- 1 / / xxx and 1 / / xxx ?

As for the absolute and relative reference. For the first result you wanted, we want to count how many time COUNTIF is > 1 for each row. We could have done that with an additional column, with =IF(COUNTIFS($A:$A, A1, $B$B, B1, $C$C, C1)>1,1,0) for each row. Then we would have SUM the results, and get what you wanted.
To do this in one single formula, we need to "simulate" this additional column with an array formula. We start with the SUM, and replace the single reference A1 (and A2, A3...) with the array A1:A10. This way, Excel knows it has to SUM the result for each cell contained in this array!

Hope this is clear^^
 
Upvote 0
Hi,

Do you want rows to match when there is empty data? For example, should this configuration match :

- 1 / aaa / xxx and 1 / / xxx ?
- 1 / / xxx and 1 / / xxx ?

As for the absolute and relative reference. For the first result you wanted, we want to count how many time COUNTIF is > 1 for each row. We could have done that with an additional column, with =IF(COUNTIFS($A:$A, A1, $B$B, B1, $C$C, C1)>1,1,0) for each row. Then we would have SUM the results, and get what you wanted.
To do this in one single formula, we need to "simulate" this additional column with an array formula. We start with the SUM, and replace the single reference A1 (and A2, A3...) with the array A1:A10. This way, Excel knows it has to SUM the result for each cell contained in this array!

Hope this is clear^^

Thanks for the excellent explanation.

Your first example would NOT match since the second column has the value aaa in the first row but no value in the other row ...
- 1 / aaa / xxx and 1 / / xxx

Your second example would match since the column values are identical for both rows ...
- 1 / / xxx and 1 / / xxx
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

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