+ Reply to Thread
Results 1 to 10 of 10

Counting Number of Unique Values in a List based on Criteria

  1. #1
    Registered User
    Join Date
    09-05-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Counting Number of Unique Values in a List based on Criteria

    I have found many solutions, both via formulas and through use of VBA, on the net for counting the number of unique values in a list.

    I want to do a count of unique values based on a criteria such that with a dataset including SiteCode and PropertyType, I want to know how many unque PropertyTypes there are with a specified SiteCode sitting alongside them.

    The PropertyTypes are strings rather than numbers and the solution will also need to cope with blanks and not count a blank as a unique value.

    My current dataset is a little over 1,000 records with 5 different SiteCodes but I will need to extend this to circa 40,000 records with circa 500 different SiteCodes.

    Given the potential future size of the dataset, the speed of calculation will also be a consideration.

    I wondered if the new "Remove Duplicates" facility on the ribbon could be used within a macro to create a temporary list either within a VBA variable or on a temporary sheet and then count the number of items in that list?

    Any pointers would be much appreciated.

    Thanks
    Last edited by greencroft; 04-03-2014 at 04:23 AM.

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Counting Number of Unique Values in a List based on Criteria

    It's possible that a Pivot table may be a solution also...depending on how the data is setup....
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Counting Number of Unique Values in a List based on Criteria

    Can you provide us with a sample file? To attach a file, push the button with the paperclip (or scroll down to the Manage Attachments button), browse to the required file, and then push the Upload button.

  4. #4
    Registered User
    Join Date
    09-05-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Counting Number of Unique Values in a List based on Criteria

    Sample data file is now attached.

    The Site Code is in column B and the Property Type in column C.

    This is just an extract of the data that will eventually be in there.

    Thanks for your interest.
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Counting Number of Unique Values in a List based on Criteria

    Does this help any?

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    09-05-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Counting Number of Unique Values in a List based on Criteria

    Quote Originally Posted by JOHN H. DAVIS View Post
    Does this help any?

    Please Login or Register  to view this content.
    Many thanks for your solution but I maybe have not sufficiently explained what I was after.

    Rather than find out how many records there are with a given Site Code and a given Property Type, I want to find out how many unique Property Types there are for a given Site Code.

    For this sample of the bigger dataset, I know that there are a total of 30 unique Property Types altogether and (doing it fairly manually) can work out that within Site Codes A to E, there are 8, 2, 21, 10 and 5 unique Property Types within each respective Site Code.

    It is these latter numbers that I want a formula or VBA solution to calculate such that I can list them in a Site Summary Table.

    For your interest, the formula I have used for calculating the number of unique values in the list is:

    =-sumproduct(-(frequency(match(datalist,datalist,0),match(datalist,datalist,0))>0))

    where datalist is the range containing the data. I unashamedly found this elsewhere on the internet (at chandoo.org) and it works beautifully for this purpose but I cannot see how to extend its methodology to introduce a criteria.

    Thanks again

  7. #7
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Counting Number of Unique Values in a List based on Criteria

    For a formula solution list A, B, C, D and E in E5 down and then in F5 use this array formula

    =SUM(IF(FREQUENCY(IF(B$5:B$1500=E5,IF(C$5:C$1500<>"",MATCH(C$5:C$1500,C$5:C$1500,0))),ROW(C$5:C$1500)-ROW(C$5)+1),1))

    confirmed with CTRL+SHIFT+ENTER and copied down to F9

    BTW for the total number of different property types you can use this formula

    =SUMPRODUCT((C$5:C$1500<>"")/COUNTIF(C$5:C$1500,C$5:C$1500&""))
    Audere est facere

  8. #8
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Counting Number of Unique Values in a List based on Criteria

    Maybe:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    09-05-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Counting Number of Unique Values in a List based on Criteria

    Quote Originally Posted by daddylonglegs View Post
    For a formula solution list A, B, C, D and E in E5 down and then in F5 use this array formula

    =SUM(IF(FREQUENCY(IF(B$5:B$1500=E5,IF(C$5:C$1500<>"",MATCH(C$5:C$1500,C$5:C$1500,0))),ROW(C$5:C$1500)-ROW(C$5)+1),1))

    confirmed with CTRL+SHIFT+ENTER and copied down to F9

    BTW for the total number of different property types you can use this formula

    =SUMPRODUCT((C$5:C$1500<>"")/COUNTIF(C$5:C$1500,C$5:C$1500&""))
    That works a treat thanks very much. I will now look at it to see if I can break down what it is doing.

    Your second formula for calculating the number of unique values in a list without criteria I had seen elsewhere but the one that used "FREQUENCY" function seemed significantly faster over a large dataset (by a factor of about 8x over 20,000 records).

    Thanks again.

  10. #10
    Registered User
    Join Date
    09-05-2012
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Counting Number of Unique Values in a List based on Criteria

    Quote Originally Posted by JOHN H. DAVIS View Post
    Maybe:

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    And thank you John for this VBA solution to the same problem.

    I want the answer to appear in a cell rather than a message box but I think even I can manage that modification.

    Great as ever to see the combined wisdom of the forum at work so promptly and the differing solutions via formulas and VBA. I am never sure which is preferable and guess it depends on whether you come from a pure spreadsheet upbringing and so are more familiar with writing esoteric formulas or were into programming languages in which cases the sequential steps in code may be the more natural approach.

    Thanks again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Counting unique text values based on criteria
    By macrorookie in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-02-2014, 09:34 AM
  2. [SOLVED] Counting Unique/Different Values based on multiple Criteria
    By jdodz in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-22-2014, 05:41 PM
  3. Counting Unique Values based on several criteria
    By GowHow in forum Excel General
    Replies: 2
    Last Post: 04-25-2012, 10:16 PM
  4. counting unique values from list with specific criteria
    By derekjames in forum Excel General
    Replies: 2
    Last Post: 12-16-2011, 06:28 PM
  5. Replies: 6
    Last Post: 10-18-2011, 07:34 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1