Solved

# count the number of duplicate and unique values in named ranges

Posted on 2014-11-21
225 Views
Dear Experts:

I would like to run a macro that ...
... counts the number of unique and duplicate values in two named ranges on the current worksheet and
... displays these values in a msgbox

Please find the detailed explanation as well as the sample file below.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas

Count-number-of-duplicate-and-unique-val
0
Question by:AndreasHermle
• 2
• 2

LVL 24

Expert Comment

Do you need it in a macro. It is possible to do this by formula.
0

Author Comment

Hi Philipp,

thank you very much for your quick feedback. As a matter of fact, I would like to use this macro along with other macros so actually the macro way would be my favourite approach. But to be honest with you, the formula solution would be very nice to know, keeping in mind that the ranges on the active worksheet can vary, i.e. the number of rows.

Regards, Andreas
0

LVL 24

Assisted Solution

Phillip Burton earned 100 total points
To find the distinct values in the first range, enter

=SUM(1/COUNTIF(B2:B9,B2:B9))

but don't press Enter. Instead, press Alt+Shift+Enter.

You should then see the answer "3".
0

LVL 92

Accepted Solution

Patrick Matthews earned 400 total points
This macro ought to do the trick:

``````Sub CountThem()

Dim rng1 As Range
Dim rng2 As Range
Dim Counter As Long
Dim coll1 As Collection
Dim coll2 As Collection

On Error Resume Next

Set rng1 = ThisWorkbook.Names("Range_1").RefersToRange
Set coll1 = New Collection
For Counter = 1 To rng1.Rows.Count
Next

Set rng2 = ThisWorkbook.Names("Range_2").RefersToRange
Set coll2 = New Collection
For Counter = 1 To rng1.Rows.Count
Next

MsgBox "Range_1 has " & coll1.Count & " distinct items and " & rng1.Rows.Count - coll1.Count & " duplicates" & _
vbCrLf & vbCrLf & _
"Range_2 has " & coll2.Count & " distinct items and " & rng2.Rows.Count - coll2.Count & " duplicates"

Set coll1 = Nothing
Set coll2 = Nothing

End Sub
``````
0

Author Closing Comment

Dear Philipp,

thank you very much for your non-macro solution which I could also use.

Patrick, as always nice and concise coding from your side. Great job!

Again thank you very much for your great, swift and professional support. I really appreciate it.

Regards, Andreas
0

## Featured Post

### Suggested Solutions

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.