我使用以下代碼將字符串列中的所有唯一值保存到數組中,然後通過計算長度來獲取唯一值的數量的陣列。VBA - 從列中獲取唯一值而不使用拆分字符串
Dim tmp As String
Dim prNumbers() As String
Dim arrLen As Integer
Dim lastRow As Integer
Dim txt As String
lastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Dim rngPR As Range
Set rngPR = Range("B2:B" & lastRow)
If Not rngPR Is Nothing Then
For Each cell In rngPR
If (cell <> "") And (InStr(tmp, cell) = 0) Then
tmp = tmp & cell & "|"
End If
Next cell
End If
If Len(tmp) > 0 Then tmp = Left(tmp, Len(tmp) - 1)
prNumbers = Split(tmp, "|")
'Find the array length
arrLen = UBound(prNumbers) + 1
然而,當運行此代碼,arrLen比唯一條目的該列中的實際量小的數目(I通過執行用於獨特條目的手動數據檢查知道這一點。)數據在列不不包含任何「|」字符,只有數字。
問題是什麼?是否有一種方法可以將唯一條目數量併入數組並確定其長度?
http://www.get-digital-help.com/2010/10/22/excel-udf-count-unique-distinct-values-in-a-large -dataset/ – Slai