Rank and Count Formula

bbalch

Board Regular
Joined
Feb 23, 2015
Messages
61
I'm trying to create a formula that will rank and count responses to survey data that will summarize the top 5 in the format below. The data will be structured similar to the table below with new responses being added to the bottom of the table continually. For this example, the data is in B2:B12.


Any suggestions on a formula that will count and rank the data as new responses are added? I'm also trying to show the results as Item (#)...for example Apple (4), Orange (3), etc. as they are ranked. See the bottom of the table below.

In the event of tie, (Pear and Plum) below they would both need to be shown.

Thanks in advance for any help / suggestions.


Favorite Food
Row 2Apple
Apple
Apple
Apple
Orange
Orange
Orange
Strawberry
Strawberry
Pear
Row 12Plum
RankCount
1Apple (4)
2Orange (3)
3Strawberry (2)
4Pear (1)
5Plum (1)

<tbody>
</tbody>
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Pivot table might be the answer rather than formulas.
Or Power Query, though I don't know anything about that.
 
Upvote 0
This is definitely not the best code but it will do. This assumes that your data is in Column A. It will output using Columns J, K, and L .
You will need to reference Microsoft Scripting Runtime
Tools
References
Microfost Scripting Runtime

Code:
Sub MoveItOver()
Application.ScreenUpdating = False
Dim dic As New Dictionary
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
Dim r As Range
Set r = Range("A2:A" & lastrow)
    For Each xcell In r
        If Not dic.Exists(xcell.Value) Then
            dic.Add xcell.Value, 1
        Else
            dic.Item(xcell.Value) = dic.Item(xcell.Value) + 1
        End If
    Next
    Dim i As Long
    Dim k
    i = 1
        For Each k In dic.Keys
        i = i + 1
            Cells(i, 10).Value = i - 1
            Cells(i, 11).Value = k & " (" & dic(k) & ")"
        Next
        Range("J1").Value = "Rank"
        Range("K1").Value = "Count"
    Set dic = Nothing
    Parseit
    Sortit
Application.ScreenUpdating = True
End Sub


Private Sub Parseit()
Dim lastrow As Long '
lastrow = Cells(Rows.Count, "K").End(xlUp).Row
Dim r As Range
Set r = Range("K2:K" & lastrow)
Dim parts() As String
    For Each xcell In r
        parts = Split(xcell.Value, "(")
        xcell.Offset(0, 1).Value = Replace(parts(1), ")", "")
    Next
End Sub


Private Sub Sortit()
Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("L1:L" & lastrow) _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("K2:L" & lastrow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("L2:L" & lastrow).Clear
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,894
Messages
6,122,124
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top