vba: get unique values from array

前端 未结 9 2309
北恋
北恋 2020-11-22 15:30

Is there any built-in functionality in vba to get unique values from a one-dimensional array? What about just getting rid of duplicates?

If not, then how would I get

9条回答
  •  面向向阳花
    2020-11-22 15:45

    There is no VBA built in functionality for removing duplicates from an array, however you could use the next function:

    Function RemoveDuplicates(MyArray As Variant) As Variant
        With CreateObject("scripting.dictionary")
            For Each item In MyArray
                c00 = .Item(item)
            Next
            sn = .keys ' the array .keys contains all unique keys
            MsgBox Join(.keys, vbLf) ' you can join the array into a string
            RemoveDuplicates = .keys ' return an array without duplicates
        End With
    End Function
    

提交回复
热议问题