Function to count distinct values in a column range

后端 未结 7 1843
孤街浪徒
孤街浪徒 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

    0 讨论(0)
提交回复
热议问题