Function to find 2nd and 3rd most common text string where blank cells are present

Deadly 提交于 2021-02-10 06:53:29

问题


I am trying to use a formula to show the 1st, 2nd, and 3rd most common text string in a column. This formula works but only if I designate a specific range with no blank cells. The issue with this is that the list is often updated by adding a line at the bottom so the range needs to be dynamic (or the entire column which is what I am trying to do).

=IFERROR(INDEX(C:C,MODE(IF(COUNTIF(U$1:U1,C:C)=0,MATCH(C:C,C:C,0)+{0,0}))),"")

Any insight is greatly appreciated.


回答1:


First enter this VBA code in a standard module:

Public Function MostCommon(rng As Range) As Variant
    Dim rng2 As Range, r As Range, C As Collection, arr(), arr2
    Dim cKount As Long, i As Long, Kaller As Range, HowBig As Long

    Set rng2 = Intersect(rng, rng.Parent.UsedRange)
    Set C = New Collection
    Set Kaller = Application.Caller

    For Each r In rng2
        If r.Value <> "" Then
            On Error Resume Next
                C.Add r.Value, CStr(r.Value)
            On Error GoTo 0
        End If
    Next r
    cKount = C.Count
    ReDim arr(1 To cKount, 1 To 2)

    For i = 1 To cKount
        arr(i, 1) = C.Item(i)
        arr(i, 2) = Application.WorksheetFunction.CountIf(rng2, arr(i, 1))
    Next i

    Call VBA_Sort(arr)

    HowBig = Application.WorksheetFunction.Max(cKount, Kaller.Rows.Count)
    ReDim arr2(1 To HowBig, 1 To 2)
    For i = 1 To HowBig
        arr2(i, 1) = ""
        arr2(i, 2) = ""
    Next i

    For i = 1 To cKount
        arr2(i, 1) = arr(i, 1)
        arr2(i, 2) = arr(i, 2)
    Next i

    MostCommon = arr2

End Function

Public Sub VBA_Sort(InOut())
    Dim i As Long, J As Long, Low As Long, _
        Hi As Long, Temp As Variant
    Low = LBound(InOut, 1)
    Hi = UBound(InOut, 1)

    J = (Hi - Low + 1) \ 2
    Do While J > 0
        For i = Low To Hi - J
          If InOut(i, 2) < InOut(i + J, 2) Then
            Temp = InOut(i, 2)
            InOut(i, 2) = InOut(i + J, 2)
            InOut(i + J, 2) = Temp
            Temp = InOut(i, 1)
            InOut(i, 1) = InOut(i + J, 1)
            InOut(i + J, 1) = Temp
          End If
        Next i
        For i = Hi - J To Low Step -1
          If InOut(i, 2) < InOut(i + J, 2) Then
            Temp = InOut(i, 2)
            InOut(i, 2) = InOut(i + J, 2)
            InOut(i + J, 2) = Temp
            Temp = InOut(i, 1)
            InOut(i, 1) = InOut(i + J, 1)
            InOut(i + J, 1) = Temp
          End If
        Next i
        J = J \ 2
    Loop
End Sub

Then select a two-column block (like E1 through F50) and array-enter the following:

=MostCommon(C:C)

As you see, the function returns a short frequency table with the most frequent items at the top.

Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key. If this is done correctly, the formula will appear with curly braces around it in the Formula Bar.



来源:https://stackoverflow.com/questions/54926474/function-to-find-2nd-and-3rd-most-common-text-string-where-blank-cells-are-prese

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!