Count unique values in Excel

前端 未结 12 2256
别那么骄傲
别那么骄傲 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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
    

提交回复
热议问题