您可以使用WorkSheetFunction.Transpose
方法将列中的数据复制到Array。它给你一个index 1 based Single Dimensional Array
。如果您有多列,则该方法为您提供多维数组。
Dim arrayV as Variant
arrayV = WorkSheetFunction.Transpose(Sheets(1).Range("A2:A20").Value)
要找到上次使用的排在这个范围内,使用下面的代码,将删除被填充到数组任何空单元格的值,
Dim LastRow as Long
LastRow = Sheets(1).Cells(Sheets(1).Rows.Count, _
Sheets(1).Range("A2:A20").Column).End(xlUp).Row
arrayV = WorkSheetFunction.Transpose(Sheets(1).Range("A2:A20").Resize(LastRow-1).Value)
接下来,以获得独特的价值观,你可能会使用一个dictionary
对象,因为它只会保存唯一的项目。
Dim dc as Object
Set dc = CreateObject("Scripting.Dictionary")
For i = Lbound(arrayV) to Ubound(arrayV)
If Not dc.Exists(arrayV(i)) Then
dc.Add arrayV(i), i
End If
Next i
'--output to Sheet or do whatever you want with this
dc.Keys() '-- gives you an array with the unique values
Set dc = Nothing