+ Reply to Thread
Results 1 to 9 of 9

Count unique values with criteria

  1. #1
    Registered User
    Join Date
    10-13-2008
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    85

    Count unique values with criteria

    Hi,

    I want to return the number of unique values with criteria, as a number. I am trying to find how many different items are in a container. The value I am using as the match is on sheet 1 - A and i want to return the value in B. Sheet 2 contains the data I am searching through eg.

    1
    MSCU
    PONU
    APHU

    2
    A MSCU
    B MSCU
    A MSCU
    A MSCU
    B PONU
    B PONU
    C APHU
    B APHU
    C APHU
    A APHU
    B APHU

    What I want to return in sheet 1 is:
    MSCU 2
    PONU 1
    APHU 3

    With the container number being the criteria of which to search. What I have been using a countif but this does not return unique values. Basically I am after a countif with unique values.

    Any help is greatly appreciated.

    Thanks

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531
    it's not entirely clear to me how sheet 2 is laid out... if we're saying the values in column A are say

    A APHU etc...

    Then I would myself be inclined to add a helper column to be on sheet2 which will in turn permit you to avoid use of expensive array formulae.

    If using your example A MSCU appears in A2 on Sheet2 (ensure you have 1 row above first record) in B2:

    =1-N(ISNUMBER(MATCH($A2,$A$1:$A1,0)))*(1-COUNTBLANK($A2))

    Copy this down for all records.

    Now on Sheet 1 you can use a standard SUMIF

    Where A1 = "MSCU", B1:

    =SUMIF('2'!$A$2:$A$100,"* "&$A1,'2'!$B$2:$B$100)

    If this is not your layout I'd advise you upload a sample of your file.

  3. #3
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    re:Count unique values with criteria

    The trick is to take the reciproque of the result of the countif function
    Please Login or Register  to view this content.
    for the rest see my attachement. Hope it helps.
    Attached Files Attached Files
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  4. #4
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561
    Use a pivot table.

    If you post an example I can show you how.

    HTH

  5. #5
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671
    Cheeky Charlie, You are so right. It's just what he needs. Stupid to overlook that one.

  6. #6
    Registered User
    Join Date
    10-13-2008
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    85
    Thanks a lot for your help everyone.

    Donkeyote - I don't entirely understand what references you are trying to make to what part of the formula, so I have added a basic format of what I have.

    In sheet1 B3-B6, I want to return the number of unique values, based on the criteria of container number (A3-A6). Please note that I have manually input the values you see, to give you a better idea of what I am trying to achieve.

    The values I have manually put in column B is the number of unique items in a container. The list of items with their respective container number are on sheet 2.

    I don't want to use a pivot table as I'm not entirely sure how to and I have 500+ lines, will I have to modify this data at all? Also, I want to return this into a different sheet than the data.

    Thanks for any help
    Attached Files Attached Files

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531
    benno87,

    use a helper cell in C on sheet2 to concatenate A & B, eg =A3&":"&B3, copy down for all rows.

    now on sheet1 using rwgrietveld sumproduct approach, your formula for Sheet1 B3 etc...

    =SUMPRODUCT(--(1/COUNTIF(Sheet2!$C$3:$C$21,Sheet2!$C$3:$C$21)),--(ISNUMBER(SEARCH($A3,Sheet2!$C$3:$C$21))))

    Where this:

    --(1/COUNTIF(Sheet2!$C$3:$C$21,Sheet2!$C$3:$C$21))

    counts the no. of unique entries in C

    and this:

    --(ISNUMBER(SEARCH($A3,Sheet2!$C$3:$C$21)))

    calculates the number of entries in C on Sheet2 that contain the criteria value...

    thus multiplying the 2 together gives you your unique count of entries where the entries contain your criteria value.

  8. #8
    Valued Forum Contributor Richard Schollar's Avatar
    Join Date
    05-23-2006
    Location
    Hampshire UK
    MS-Off Ver
    Excel 2002
    Posts
    1,264
    An alternative approach is to use Frequency - see attached. FOrmula is:

    =SUM(IF(FREQUENCY(IF(Sheet2!$B$3:$B$21=A3,MATCH(Sheet2!$A$3:$A$21&Sheet2!$B$3:$B$21,Sheet2!$A$3:$A$21&Sheet2!$B$3:$B$21,0)),MATCH(Sheet2!$A$3:$A$21&Sheet2!$B$3:$B$21,Sheet2!$A$3:$A$21&Sheet2!$B$3:$B$21,0))>0,1))

    copied down. Confirmed with Ctrl+Shift+Enter.

    Richard
    Attached Files Attached Files
    Richard Schollar
    Microsoft MVP - Excel

  9. #9
    Registered User
    Join Date
    10-13-2008
    Location
    Australia
    MS-Off Ver
    2007
    Posts
    85

    Thanks

    Thanks for all your help everyone, even if I didn't end up using a helper cell or pivot table, I appreciate your time. These ways seemed a bit more difficult than an equation.

    RichardSchollar - Your formula does exactly what I wanted to do, thanks a lot for your help.

    Cheers

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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