设在单元格A1至A5我有以下的,想唯一值即{A,B,C,d}
A
1 "a"
2 "b"
3 "c"
4 "c"
5 "d"
后续的代码将有助于实现这一两件组成的数组:
CreateUniqueArray - 从每个细胞获得VAL,如果不是已经在阵列添加到阵列
IsInArray - 效用函数,以检查是否在数组值通过执行简单的循环
我不得不说,这是蛮力方式,并欢迎任何改善...
Sub Test()
Dim firstRow As Integer, lastRow As Integer, cnt As Integer, iCell As Integer
Dim myArray()
cnt = 0
firstRow = 1
lastRow = 10
For iCell = firstRow To lastRow
If Not IsInArray(myArray, Cells(iCell, 1)) Then
ReDim Preserve myArray(cnt)
myArray(cnt) = Cells(iCell, 1)
cnt = cnt + 1
End If
Next iCell
End Sub
Function IsInArray(myArray As Variant, val As String) As Boolean
Dim i As Integer, found As Boolean
found = False
If Not Len(Join(myArray)) > 0 Then
found = False
Else
For i = 0 To UBound(myArray)
If myArray(i) = val Then
found = True
End If
Next i
End If
IsInArray = found
End Function
您可以使用字典采取独特的价值观,然后将它们复制回你的阵列(或直接使用字典,你喜欢的方式)。注意这个问题已经回答[这里](http://stackoverflow.com/questions/3017852/vba-get-unique-values-from-array)。 RGDS – 2011-03-21 19:28:39