Function to count distinct values in a column range

后端 未结 7 1846
孤街浪徒
孤街浪徒 2021-01-06 19:35

I am attempting to create a function in VBA that, when given a range of values, will return a Count Distinct of those values. For example:

| Column A | |-----

7条回答
  •  执笔经年
    2021-01-06 20:07

    I'll chime in here as well...

    Public Function Count_Distinct_In_Column(Rng As Range)
        Count_Distinct_In_Column = _
        Evaluate("Sum(N(countif(offset(" & Rng.Cells(1).Address _
        & ",,,row(" & Rng.Address & "))," & Rng.Address & ")=1))")
    End Function
    

    Called like:

     ? Count_Distinct_In_Column(Range("A2:A12"))
    

    6

提交回复
热议问题