我目前使用以下公式来查找范围内最常见的单词或数字:

=INDEX(E9:E18,MODE(MATCH(E9:E18,E9:E18,0)))

但是,如果有任何空白单元格,则该公式将返回一个空白单元格作为模式。如何修改它以找到最常见的单词/数字而忽略任何空白单元格?

非常感谢

最佳答案

尝试以下用户定义函数:

Public Function MostFreq(rIn As Range) As Variant
    Dim c As Collection, r As Range, N As Long, How()
    Dim cc As Long, wf As WorksheetFunction
    Dim i As Long, Biggest As Long
    Set c = New Collection
    Set wf = Application.WorksheetFunction

    On Error Resume Next
    For Each r In rIn
        v = r.Text
        If v <> "" Then
        c.Add v, CStr(v)
        End If
    Next r

    On Error GoTo 0
    cc = c.Count
    ReDim How(1 To cc)

    For i = 1 To cc
        How(i) = wf.CountIf(rIn, c.Item(i))
    Next i
    Biggest = wf.Max(How)
    For i = 1 To cc
        If How(i) = Biggest Then
            MostFreq = c.Item(i)
        End If
    Next i
End Function

为了避免 VBA, 选择一个单元格(比如 A1)并输入数组公式:
=INDEX(E9:E18,MODE(IF((E9:E18<>"")*ISNA(MATCH(E9:E18,$B$1:$B1,0)),MATCH(E9:E18,E9:E18,0))))

数组公式 必须使用 Ctrl + Shift + Enter 输入,而不仅仅是 Enter 键。

下面是一个例子:

关于excel - 范围内最频繁的单词,忽略空格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26868704/

10-12 17:59