一尘不染

VBA:从数组获取唯一值

algorithm

vba中是否有任何内置功能可从一维数组获取唯一值?只是消除重复呢?

如果没有,那么我如何从数组中获取唯一值?


阅读 1229

收藏
2020-07-28

共1个答案

一尘不染

这篇文章包含2个示例。我喜欢第二个:

Sub unique() 
  Dim arr As New Collection, a 
  Dim aFirstArray() As Variant 
  Dim i As Long

  aFirstArray() = Array("Banana", "Apple", "Orange", "Tomato", "Apple", _ 
  "Lemon", "Lime", "Lime", "Apple")

  On Error Resume Next 
  For Each a In aFirstArray 
     arr.Add a, a 
  Next

  For i = 1 To arr.Count 
     Cells(i, 1) = arr(i) 
  Next

End Sub
2020-07-28