Solved

count the number of duplicate and unique values in named ranges

Posted on 2014-11-21
5
230 Views
Last Modified: 2014-11-22
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_duplicate_unique_values
 Count-number-of-duplicate-and-unique-val
0
Comment
Question by:AndreasHermle
  • 2
  • 2
5 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40457779
Do you need it in a macro. It is possible to do this by formula.
0
 

Author Comment

by:AndreasHermle
ID: 40457804
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

by:Phillip Burton
Phillip Burton earned 100 total points
ID: 40457828
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

by:
Patrick Matthews earned 400 total points
ID: 40457897
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
        coll1.Add rng1.Cells(Counter, 2).Value, rng1.Cells(Counter, 2).Value
    Next
    
    Set rng2 = ThisWorkbook.Names("Range_2").RefersToRange
    Set coll2 = New Collection
    For Counter = 1 To rng1.Rows.Count
        coll2.Add rng2.Cells(Counter, 2).Value, rng2.Cells(Counter, 2).Value
    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

Open in new window

0
 

Author Closing Comment

by:AndreasHermle
ID: 40459428
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now