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
 
Thanks Aladin, that is impressive and works as required. I understand you are concatenating the column values with a pipe character in the MATCH and that makes sense to me but not sure how the next bit works in the FREQUENCY ... ROW(A1:A9)-ROW(A1)+1
Are you able to briefly shed some light on that? It is a bit hard for me to visualize how that works in an array formula.

Thanks also to NotoriousPopol for your helpful suggestions.

Regards
Deutz

We have 3 vertical ranges: Let symbolize them with X, Y, and Z.

Let X|Y|Z >> W. The bar separator is used in order not to confound 01|0 and 0|10 (without the |, we would have just 010). Note that this distinction is needed for the MATCH bit, but not for the LEN bit.

The basic bit in the proposed formulas is:

IF(FREQUENCY(IF(LEN(W),MATCH(W,W,0)),ROW(W)-ROW(INDEX(W,1,1))+1),1)

Intermezzo: ROW(A2:A4)-ROW(A2)+1 is equal to ROW(A2:A4)-ROW(INDEX(A2:A4,1,1))+1. The INDEX bit just picks out the first element of the range in question.

ROW(W)-ROW(INDEX(W,1,1))+1 is the bin array argument the FREQUENCY functions must have.

This ROW expression creates an integer array that starts with 1 and ends with N which is equal to the size of the W range.

Given the foregoing, the following link should help you out how the basic bit works:

http://www.mrexcel.com/forum/excel-...ing-sum-if-frequency-match-2.html#post3156949

Additional intermezzo:

W <> ""
LEN(W)>0
1-(W="")

are all equivalent.
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
We have 3 vertical ranges: Let symbolize them with X, Y, and Z.

Let X|Y|Z >> W. The bar separator is used in order not to confound 01|0 and 0|10 (without the |, we would have just 010). Note that this distinction is needed for the MATCH bit, but not for the LEN bit.

The basic bit in the proposed formulas is:

IF(FREQUENCY(IF(LEN(W),MATCH(W,W,0)),ROW(W)-ROW(INDEX(W,1,1))+1),1)

Intermezzo: ROW(A2:A4)-ROW(A2)+1 is equal to ROW(A2:A4)-ROW(INDEX(A2:A4,1,1))+1. The INDEX bit just picks out the first element of the range in question.

ROW(W)-ROW(INDEX(W,1,1))+1 is the bin array argument the FREQUENCY functions must have.

This ROW expression creates an integer array that starts with 1 and ends with N which is equal to the size of the W range.

Given the foregoing, the following link should help you out how the basic bit works:

http://www.mrexcel.com/forum/excel-...ing-sum-if-frequency-match-2.html#post3156949

Additional intermezzo:

W <> ""
LEN(W)>0
1-(W="")

are all equivalent.

Thanks again Aladin

That was so much more than I expected.

I'll have to take some time to digest that.

Regards
Deutz
 
Upvote 0
Another option that you may wish to consider is a user-defined function.

Advantage:
- Simpler formula on the worksheet itself
- Easily expand to more columns if required
Disadvantage:
- User must have macros enabled

To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula(s) as shown in the screen shot below.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

Rich (BB code):
Function CountDupes(r As Range, Optional bExcludeFirst As Boolean, Optional bMatchCase As Boolean) As Long
  Dim a As Variant, e As Variant
  Dim d As Object
  Dim i As Long
  Dim s As String
  
  a = r.Value
  Set d = CreateObject("Scripting.dictionary")
  If Not bMatchCase Then d.comparemode = 1
  For i = 1 To UBound(a)
    s = Join(Application.Index(a, i, 0), "|")
    If Len(s) >= UBound(a, 2) Then d(s) = d(s) + 1
  Next i
  For Each e In d.Items
    If e > 1 Then CountDupes = CountDupes + e - IIf(bExcludeFirst, 1, 0)
  Next e
End Function

Examples of use below.
This function has an added option as to whether to count items as equal if their case (upper/lower) is identical or not - see cell E3
For the two option arguments in the function, you can use True/False or 1/0. If omitted, those arguments will be treated as False)
If more columns are to be included, just change the range in the function on the worksheet. eg for 10 columns =CountDupes(A1:J9)


Excel 2010 32 bit
ABCDE
11aaax5
21aaax3
34
44e
52r
6
7zz
82r
91AAAx
Count Dupes
Cell Formulas
RangeFormula
E1=CountDupes(A1:C9)
E2=CountDupes(A1:C9,TRUE)
E3=CountDupes(A1:C9,0,1)
 
Last edited:
Upvote 0
Another option that you may wish to consider is a user-defined function.

Advantage:
- Simpler formula on the worksheet itself
- Easily expand to more columns if required
Disadvantage:
- User must have macros enabled

To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula(s) as shown in the screen shot below.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

Rich (BB code):
Function CountDupes(r As Range, Optional bExcludeFirst As Boolean, Optional bMatchCase As Boolean) As Long
  Dim a As Variant, e As Variant
  Dim d As Object
  Dim i As Long
  Dim s As String
  
  a = r.Value
  Set d = CreateObject("Scripting.dictionary")
  If Not bMatchCase Then d.comparemode = 1
  For i = 1 To UBound(a)
    s = Join(Application.Index(a, i, 0), "|")
    If Len(s) >= UBound(a, 2) Then d(s) = d(s) + 1
  Next i
  For Each e In d.Items
    If e > 1 Then CountDupes = CountDupes + e - IIf(bExcludeFirst, 1, 0)
  Next e
End Function

Examples of use below.
This function has an added option as to whether to count items as equal if their case (upper/lower) is identical or not - see cell E3
For the two option arguments in the function, you can use True/False or 1/0. If omitted, those arguments will be treated as False)
If more columns are to be included, just change the range in the function on the worksheet. eg for 10 columns =CountDupes(A1:J9)

Excel 2010 32 bit
ABCDE
11aaax5
21aaax3
34
44e
52r
6
7zz
82r
91AAAx

<colgroup><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Count Dupes

Worksheet Formulas
CellFormula
E1=CountDupes(A1:C9)
E2=CountDupes(A1:C9,TRUE)
E3=CountDupes(A1:C9,0,1)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

Thanks for that Peter. As you say, the big advantage is if you have a lot of columns that could make the formula method unwieldy. That UDF is a very elegant.

Regards
Deutz
 
Upvote 0
Thanks for that Peter. As you say, the big advantage is if you have a lot of columns that could make the formula method unwieldy. That UDF is a very elegant.

Regards
Deutz
You are very welcome.

(BTW, best not to quote whole long posts. It tends to make the post & thread harder to read & navigate for no real advantage.)
 
Upvote 0
Sorry about the long post quote. I'll avoid that in future. Thanks again Peter.

Regards
Deutz
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,893
Members
449,194
Latest member
JayEggleton

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