Count unique values in Excel

前端 未结 12 2195
别那么骄傲
别那么骄傲 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:48

    Look at https://excelchamps.com/blog/count-unique-values-excel/. There is your answer.

    The formula you need to enter is:

    =SUMPRODUCT(1/COUNTIF(C2:C2080,C2:C2080))
    

    When you enter this formula as an array it will look something like this:

    {=SUMPRODUCT(1/COUNTIF(C2:C2080,C2:C2080))}
    
    0 讨论(0)
  • 2020-11-28 12:51

    JustinG's function works very well (and fast) until the number of unique items exceeds 32,767 due to some type of limit in Excel.

    I found if you modify his code

    Public Function CountUnique(rng As Range) As Integer
    

    and make it as...

    Public Function CountUnique(rng As Range) As Long
    

    It will then handle more unique items.

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

    This might be a more efficient way of dealing with a large number of rows. This uses the inbuilt AdvancedFilter command instead of cycling through each cell at a time.

    Public Function UniqueValues(oRange As Range) As Variant
    
    ' Uses the built-in AdvancedFilter Excel command to return the unique values onto the Worksheet
    ' and then populate and retuns an array of unique values
    
    ' Note:  The index:0 element in the returned array will be the header row.
    '        You can ignore this element unless the first row in your oRange is a unique value
    '        in which case the header will be that value.
    
    Dim oTarget As Range
    Dim r As Long, numrows As Long
    Dim vs1, vs2 As Variant
    
    ' Get the first unused cell on the first row where the unique vaues will be temporarily populated
       Set oTarget = oRange.SpecialCells(xlLastCell) ' the last cell on the worksheet
       Set oTarget = oTarget.Parent.Cells(1, oTarget.Column + 1) ' the first unused cell on the first row
    
    ' Copy the unique values from your oRange to the first unused cell on row 1
       oRange.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=oTarget, Unique:=True
    
    ' Get the number of rows including the first row which is the header
       numrows = WorksheetFunction.CountA(oTarget.EntireColumn)
    
    ' create an 2-dim array of the rows
       vs1 = oTarget.Resize(numrows)
    
    ' Prepare a second 1-dim array for the result
       ReDim vs2(numrows)
    
    ' Transfer the 2-dim array into the 1-dim array
       For r = 1 To UBound(vs1, 1)
          vs2(r - 1) = vs1(r, 1)
       Next
    
    ' Return the 1-dim array as the function result
       UniqueValues = vs2
    
    ' Clean up the extra column on the worksheet
       oTarget.EntireColumn.Delete
    
    End Function
    
    0 讨论(0)
  • 2020-11-28 12:54

    Since fall 2018, this problem can be solved much easier using the new dynamic array functions (currently only available for Office 365 clients - this solution does not work on Excel 2016 / 2019):

    =COUNTA(UNIQUE(C2:C2080))
    

    The UNIQUE function will result in an array of unique values in the range, which can be counted using COUNTA.

    If you have blanks in your range, you may filter them out using the FILTER function:

    =COUNTA(UNIQUE(FILTER(C2:C2080,C2:C2080<>"")))
    
    0 讨论(0)
  • 2020-11-28 12:56

    You could also simply use a filter to temporarily display unique values, and count the filtered values.

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

    For anyone still trying to use @JustinG's dictionary method, you will need to alter the code slightly if you're using a newer version of VBA.

    You'll need to reference 'Microsoft Scripting Runtime' and prefix the Dictionary terms with Scripting, as follows:

    Public Function CountUnique(rng As Range) As Long
        Dim dict As Scripting.Dictionary
        Dim cell As Range
        Set dict = New Scripting.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)
提交回复
热议问题