简体   繁体   中英

Counting Unique Values for Multiple Columns in Excel

I've attempted using Pivot tables and SUMPRODUCT & COUNTIF formulas after looking through possible solutions but haven't found anything positive yet. Below is the input data:

Level 1     Level 2     Level 3     Level 4     Level 5
Tom         Liz         
Tom         Liz         Mel     
Tom         Liz         Dan     
Tom         Liz         Dan         Ian
Tom         Liz         Dan         Ken 
Tom         Tim         
Tom         Tim         Fab     
Tom         Tim         Fab         Ken 
Tom         Tim         Fab         Ken          Jan
Eve             

Expected output data is below. The intent is to not have to feed in a pre-loaded list of names. The expectation is that the program could determine the counts based on the input data alone:

Counts
-------
Tom: 9
Eve: 1
Liz: 5
Tim: 4
Mel: 1
Dan: 3
Fab: 3
Ian: 1
Ken: 3
Jan: 1

Any help towards this is appreciated....thanks!

UPDATE: A preloaded list with the list of Names CAN be used to generate the counts. The above description was updated accordingly.

First enter the following UDF in a standard module:

Public Function ListUniques(rng As Range) As Variant
    Dim r As Range, ary(1 To 9999, 1 To 1) As Variant
    Dim i As Long, C As Collection
    Set C = New Collection
    On Error Resume Next
    For Each r In rng
        v = r.Value
        If v <> "" Then
            C.Add v, CStr(v)
        End If
    Next r
    On Error GoTo 0

    For i = 1 To 9999
        If i > C.Count Then
            ary(i, 1) = ""
        Else
            ary(i, 1) = C.Item(i)
        End If
    Next i
    ListUniques = ary
End Function

Then hi-light a section of a column, say G1 thru G50 and enter the Array Formula:

=listuniques(A2:E11)

Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.

If done correctly you should see something like:

PIC



Finally in H1 enter:

=COUNTIF($A$2:$E$11,G1)

and copy down

NOTE

User Defined Functions (UDFs) are very easy to install and use:

  1. ALT-F11 brings up the VBE window
  2. ALT-I ALT-M opens a fresh module
  3. paste the stuff in and close the VBE window

If you save the workbook, the UDF will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

To remove the UDF:

  1. bring up the VBE window as above
  2. clear the code out
  3. close the VBE window

To use the UDF from Excel:

=myfunction(A1)

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

and for specifics on UDFs, see:

http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

Macros must be enabled for this to work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM