0
我用下面的函數來收集最頻繁的構件在陣列列:VBA在一個維字符串數組斂最小重複memeber(S)
Function MosFreqinsimplearr(ByRef arrin As Variant, colindx As Integer) As Variant
Dim i As Integer
Set dic = CreateObject("scripting.dictionary")
On Error Resume Next
xMax = 0
xOutValue = ""
For i = 1 To UBound(arrin)
xValue = arrin(i, colindx)
If xValue <> "" Then
dic(xValue) = dic(xValue) + 1
xCount = dic(xValue)
If xCount > xMax Then
xMax = xCount
xOutValue = xValue
End If
End If
Next i
MosFreqinsimplearr = xOutValue
Set dic = Nothing
End Function
1-我需要程序返回最小重複或較不頻繁的構件(S)。似乎同樣的程序不能被用於獲取這樣的結果,如我嘗試:
Dim dic As Object
Dim j As Integer
Dim xMin As Integer
Dim xOutValue As String
Dim xValue As String
Dim xCount As Integer
Dim ar(1 To 11) As Variant
ar(1) = "banana"
ar(2) = "banana"
ar(3) = "banana"
ar(4) = "apple"
ar(5) = "apple"
ar(6) = "banana"
ar(7) = "cucumber"
ar(8) = "cucumber"
ar(9) = "cucumber"
ar(10) = "apple"
ar(11) = "cucumber"
Set dic = CreateObject("scripting.dictionary")
'On Error Resume Next
xMin = 0
xOutValue = ""
For j = 1 To UBound(ar)
xValue = ar(j)
If xValue <> "" Then
dic(xValue) = dic(xValue) + 1
xCount = dic(xValue)
If xCount <= xMin Then
xMin = xCount
xOutValue = xValue
Else: xOutValue = xValue
End If
End If
Next j
MsgBox "less repeated value is:" & vbTab & xOutValue
Set dic = Nothing
2-什麼是每個唯一值的計數數的代碼:
banana=4
cucumber=4
apple=3
問候;
查看[排序集合/詞典](http://www.cpearson.com/excel/CollectionsAndDictionaries.htm)? – QHarr