VBA功能返回' #VALUE!'

时间:2018-04-27 15:02:25

标签: arrays excel vba mode

Public Function MostOccuring(items() As Variant) As String
    Dim count() As Integer
    Dim strings() As Object
    Dim Index As Integer
    For Index = 0 To items.Length - 1
        If srings.Exists(items(Index)) Then
            count(strings.IndexOf(items(Index))) = 1 + count(strings.IndexOf(items(Index)))
        Else
            count(Index) = 1
            strings(Index) = items(Index)
        End If
    Next
    End
    MostOccuring = strings(count.IndexOf(count.Max()))

End Function

这是我最常用的功能
This is how I call it

这就是我所说的

它返回' #VALUE!'。为什么?它应该返回最常出现的单元格字符串。感谢。

1 个答案:

答案 0 :(得分:0)

您也可以尝试这样的事情......

Public Function MostOccuring(items As Range) As String
Dim cell As Range
Dim dict, it
Dim maxCnt As Long

Set dict = CreateObject("Scripting.Dictionary")
For Each cell In items
    If Not dict.exists(cell.Value) Then
        dict.Item(cell.Value) = 1
    Else
        dict.Item(cell.Value) = dict.Item(cell.Value) + 1
    End If
Next cell

For Each it In dict.keys
    If dict.Item(it) > maxCnt Then
        maxCnt = dict.Item(it)
        MostOccuring = it
    End If
Next it
End Function