Unique values from 1D-array, without iteration

后端 未结 4 767
陌清茗
陌清茗 2021-01-13 19:25

At risk of being of topic, I decided to share some code, Q&A-style. If the general opinion is such that this would be off-topic I\'ll be happy to delete if need be.

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 19:56

    Applying the Unique() function without double transposition (Office 365)

    As addition to @ScottCraner 's Office 365 solution an alternative without need to transpose twice:

    Sub testUniques()
    Dim arr:     arr = Array("A", "A", "C", "D", "A", "E", "G")  ' example data
    Dim uniques: uniques = Application.Unique(arr, True)         ' return function result
    'optional display in VB Editor's immediate window
        Debug.Print Join(arr, ",") & " ~> " & Join(uniques, ",") ' A,A,C,D,A,E,G ~> A,C,D,E,G
    End Sub
    

    Explanation to additional argument by_col

    Due to the Unique function reference its syntax is UNIQUE(array,[by_col],[exactly_once]), where

    "the by_col argument is a logical value indicating how to compare. TRUE will compare columns against each other and return the unique columns."

    Setting the by_col argument to True allows to compare the array items against each other as they are considered as "columns" in a "flat" 1-dimensional array.

提交回复
热议问题