Public Function MostCommonInArray(ByRef SrcArray() As Variant) As String
[COLOR="DarkGreen"]'*********************************************************************************
'Returns the most common item in a single dimensional array **
'and the number of those items delimited by a Pipe (|) symbol **
'
'Example of how to use:
'=====================
'
'Dim MyArray(20) As Variant
'Dim StrgResult As String, Cnt As Long, StrgItem As String
'MyArray(0) = 1: MyArray(1) = "": MyArray(2) = 3: MyArray(3) = 3: MyArray(4) = 3
'MyArray(5) = 3: MyArray(6) = 4: MyArray(7) = 5: MyArray(8) = 6: MyArray(9) = 6
'MyArray(10) = 3: MyArray(11) = 4: MyArray(12) = 4: MyArray(13) = 6: MyArray(14) = 7
'MyArray(15) = 3: MyArray(16) = 4: MyArray(17) = 5: MyArray(18) = 6: MyArray(19) = 3
'MyArray(20) = 8
'StrgResult = MostCommonInArray(MyArray())
'StrgItem = Left$(StrgResult, InStr(StrgResult, " |") - 1)
'Cnt = CLng(Mid$(StrgResult, InStr(StrgResult, "| ") + 2))
'MsgBox "The Item " & StrgItem & " is most common within the Array." & _
' vbCr & "It is found within " & Cnt & " Array elements.", vbInformation, _
' "Most Common In Array"
'*********************************************************************************[/COLOR]
Dim TmpArray() As Variant
Dim i As Long, j As Long, x As Long
Dim Index As Long
Dim CntArray() As String
[COLOR="DarkGreen"] 'Set the mouse pointer to Busy for Arrays larger than 300 elements.
'You can set this to whatever you want.[/COLOR]
If UBound(SrcArray) > 300 Then Screen.MousePointer = 11
[COLOR="DarkGreen"]'Trap Errors[/COLOR]
On Error Resume Next
For i = LBound(SrcArray) To UBound(SrcArray)
[COLOR="DarkGreen"]'If there is a problem with the array passed then get
'outta here and return "-1 | -1" to indicate a error.[/COLOR]
If Err <> 0 Then Err.Clear: MostCommonInArray = "-1 | -1": Exit For
[COLOR="DarkGreen"]'Trim any leading or trailing spaces (if any)
'from the Array element but ONLY if the Element
'contains a String value.[/COLOR]
If TypeName(SrcArray(i)) = "String" Then SrcArray(i) = Trim(SrcArray(i))
[COLOR="DarkGreen"] 'Does the TmpArray array contain anything?
'(an error is used to detect this)[/COLOR]
j = UBound(TmpArray)
[COLOR="DarkGreen"]'If it does let's carry on...[/COLOR]
If Err = 0 Then
For j = LBound(TmpArray) To UBound(TmpArray)
[COLOR="DarkGreen"]'Make sure we're not working with a blank array element.[/COLOR]
If Len(SrcArray(i)) = 0 Then Exit For
If Left$(TmpArray(j), IIf(InStr(1, TmpArray(j), ",") > 0, InStr(1, TmpArray(j), ",") - 1, Len(TmpArray(j)))) = SrcArray(i) Then
If Right$(TmpArray(j), 1) <> "," Then TmpArray(j) = TmpArray(j) & ","
TmpArray(j) = TmpArray(j) & SrcArray(i)
Index = 1: Exit For
End If
Next j
If Index = 0 Then
ReDim Preserve TmpArray(x)
TmpArray(x) = SrcArray(i)
x = x + 1
End If
Else
[COLOR="DarkGreen"] 'The TmpArray Array doesn't contain anything yet so
'let's create the first element after we clear the
'error.[/COLOR]
Err.Clear: x = 0
ReDim Preserve TmpArray(x)
TmpArray(x) = SrcArray(i)
x = x + 1
End If
Next i
[COLOR="DarkGreen"] 'Let's see which array element in TmpArray has the most items
'and Return that info to the calling procedure.[/COLOR]
x = 0: Index = 0
For i = LBound(TmpArray) To UBound(TmpArray)
CntArray = Split(TmpArray(i), ",")
Index = UBound(CntArray) + 1
If Index > x Then MostCommonInArray = CntArray(0): x = Index
Next i
MostCommonInArray = MostCommonInArray & " | " & x
[COLOR="DarkGreen"]'Kill the temporary Arrays used in this Function.[/COLOR]
Erase TmpArray, CntArray
[COLOR="DarkGreen"]'Set the mouse pointer to normal[/COLOR]
Screen.MousePointer = 0
End Function