2017-12-27 11 views
0

I Funktion folgende verwendet häufigste Element in Array-Spalte zu sammeln:Vba Mindest wiederholte memeber erregende (s) in einer eindimensionalen String-Array

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- Ich brauche Verfahren zur Rückführung mindestens wiederholten oder weniger häufigen Mitglied (er). es scheint, dass gleiche Verfahren nicht für immer ein solches Ergebnis verwendet werden könnte, wie ich versucht:

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-was ist die Codenummer eines jeden eindeutigen Wert für das Zählen:

banana=4 
cucumber=4 
apple=3 

Grüße;

+0

Schauen Sie in [Sortieren von Sammlungen/Wörterbüchern] (http://www.cpearson.com/excel/CollectionsAndDictionaries.htm)? – QHarr

Antwort

0

Sie müssen über die Schlüssel des Wörterbuchs iterieren und die Werte vergleichen.

Dim key As Variant, minKey As Variant 

For Each key In dic 
    If IsEmpty(minKey) Then 
     minKey = key 
    Else 
     If dic(key) < dic(minKey) Then minKey = key 
    End If 
Next 
Verwandte Themen