Function Top5(myArray As Variant) As Variant 'accepts an array to look at and returns an array
Dim uniqueArray() As Variant
Dim counterArray() As Integer
uniqueArray = Array()
a = 1
For Each Item In myArray
itExists = False
For Each element In uniqueArray
If Item = element Then
itExists = True
End If
Next element
If itExists = False Then
ReDim Preserve uniqueArray(a)
uniqueArray(a) = Item
a = a + 1
End If
Next Item
For x = LBound(uniqueArray) To UBound(uniqueArray)
ReDim Preserve counterArray(x)
counterArray(x) = 0
Next
a = 0
For Each element In uniqueArray
For Each Item In myArray
If element = Item Then
counterArray(a) = counterArray(a) + 1
End If
Next Item
a = a + 1
Next element
a = 0
Top1 = 0
Top2 = 0
Top3 = 0
Top4 = 0
Top5 = 0
For Each thingy In uniqueArray
If counterArray(a) > Top1 Then
Top5 = Top4
Top4 = Top3
Top3 = Top2
Top2 = Top1
Top1 = counterArray(a)
Top5Value = Top4Value
Top4Value = Top3Value
Top3Value = Top2Value
Top2Value = Top1Value
Top1Value = thingy
ElseIf counterArray(a) > Top2 Then
Top5 = Top4
Top4 = Top3
Top3 = Top2
Top2 = counterArray(a)
Top5Value = Top4Value
Top4Value = Top3Value
Top3Value = Top2Value
Top2Value = thingy
ElseIf counterArray(a) > Top3 Then
Top5 = Top4
Top4 = Top3
Top3 = counterArray(a)
Top5Value = Top4Value
Top4Value = Top3Value
Top3Value = thingy
ElseIf counterArray(a) > Top4 Then
Top5 = Top4
Top4 = counterArray(a)
Top5Value = Top4Value
Top4Value = thingy
ElseIf counterArray(a) > Top5 Then
Top5 = counterArray(a)
Top5Value = thingy
End If
a = a + 1
Next thingy
Top5 = Array(Top1Value, Top2Value, Top3Value, Top4Value, Top5Value) ' store results in array
End Function