Count unique values in Excel

前端 未结 12 2185
别那么骄傲
别那么骄傲 2020-11-28 12:28

I need to count unique values in range (C2:C2080) in excel. Googled formula:

=SUM(IF(FREQUENCY(MATCH(C2:C2080;C2:C2080;0);MATCH(C2:C280;C2:C2080;0))>0;1))         


        
相关标签:
12条回答
  • 2020-11-28 12:38

    Try:

    =SUM(IF(FREQUENCY(C2:C2080,C2:C2080)>0,1))

    EDIT: The above will handle blank entries in the column

    0 讨论(0)
  • 2020-11-28 12:40

    Here is a VBA function that works for me.

    You can use it as a worksheet function, referencing any range, eg “=CountUnique(N8:O9)”

    It handles text and numeric values, and treats blank cells as one value

    It does not require dealing with array functions.

    It requires a reference to the Microsoft Scripting Library, for the dictionary object.

        Public Function CountUnique(rng As Range) As Integer
            Dim dict As Dictionary
            Dim cell As Range
            Set dict = New Dictionary
            For Each cell In rng.Cells
                 If Not dict.Exists(cell.Value) Then
                    dict.Add cell.Value, 0
                End If
            Next
            CountUnique = dict.Count
        End Function
    
    0 讨论(0)
  • 2020-11-28 12:41
    
    =SUM(IF(FREQUENCY(IF(LEN(A2:A10)>0,MATCH(A2:A10,A2:A10,0),""), IF(LEN(A2:A10)>0,MATCH(A2:A10,A2:A10,0),""))>0,1)) 
    

    http://office.microsoft.com/en-us/excel/HP030561181033.aspx

    You may also write a VBA macro (not sure if that's what you're after though.)

    Something to the effect of (given a spreadsheet with A1-A11 filled and B1-B11 empty):

    Sub CountUnique()
    
    Dim count As Integer
    Dim i, c, j As Integer
    
    c = 0
    count = 0
    For i = 1 To 11
        Sheet1.Cells(i, 2).Value = Sheet1.Cells(i, 1).Value
        c = c + 1
        For j = 1 To c
            If CDbl(Sheet1.Cells(i, 1).Value) = CDbl(Sheet1.Cells(j, 2).Value) Then
                c = c - 1
                Exit For
            End If
        Next j
    Next i
    
    ' c now equals the unique item count put in the 12'th row
    Sheet1.Cells(12, 1).Value = c
    
    End Sub
    
    0 讨论(0)
  • 2020-11-28 12:45

    The formula works for me. There are a few things that could cause this to not work. First, all target cells must have a value in them. Another example of where this might not work is if you have one cell with the value 31 and another cell with a text value of "31". It will recognize these as different values.

    You could try this:

    =SUM(IF(FREQUENCY(IF(LEN(B2:B11)>0,MATCH(B2:B11,B2:B11,0),""), IF(LEN(B2:B11)>0,MATCH(B2:B11,B2:B11,0),""))>0,1))
    

    This is an array formula. Instead of hitting just enter to confirm it you must hit ctrl+shift+enter.

    Which is from:

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

    0 讨论(0)
  • 2020-11-28 12:45

    Another way to do this is this:

    Sub CountUnique()
        Dim Count, x, a, lastRow, Values(), StringValues
        a = ActiveCell.Column
        a = GetLetterFromNumber(a)
        lastRow = Range(a & Rows.Count).End(xlUp).row
        Count = 0
        For Each c In Range(Range(a & "1"), Range(a & Rows.Count).End(xlUp))
            If c.row = 1 Then
                ReDim Values(lastRow)
                Values(Count) = c.Value
                Count = Count + 1
            End If
            StringValues = Join(Values, "#")
            StringValues = "#" + StringValues
            If InStr(1, StringValues, c.Value) = 0 Then
                Values(Count) = c.Value
                Count = Count + 1
            End If
        Next c
        MsgBox "There are " & Count & " unique values in column " & a
    End Sub
    

    You just have to have the active cell be on row 1 of the column that you are counting.

    0 讨论(0)
  • 2020-11-28 12:48

    After reading through this and then investigating further, I've got one that works better for me than anything I see here:

    Array-enter:
    (Ctrl+Shift+Enter, and don't include the curly brackets)

    {=SUM(IFERROR(1/COUNTIF(C2:C2080,C2:C2080),0))}
    

    Or in VBA:

    MyResult = MyWorksheetObj.Evaluate("=SUM(IFERROR(1/COUNTIF(C2:C2080,C2:C2080),0))")
    

    It works for both numbers and text, it handles blank cells, it handles errors in referenced cells, and it works in VBA. It's also one of the most compact solutions I've seen. Using it in VBA, it apparently automatically handles the need to be an array formula.

    Note, the way it handles errors is by simply including them in the count of uniques. For example, if you have two cells returning #DIV/0! and three cells returning #VALUE!, those 5 cells would add 2 to the final count of unique values. If you want errors completely excluded, it would need to be modified for that.

    In my tests, this one from Jacob above only works for numbers, not text, and does not handle errors in referenced cells (returns an error if any of the referenced cells returns an error):

    =SUM(IF(FREQUENCY(G4:G29,G4:G29)>0,1))
    
    0 讨论(0)
提交回复
热议问题