VBA help to identify most frequently occurring text

L

Legacy 462862

Guest
Hi,

I've used the following to identify the most frequently occurring text in my sheet:

- - -

Function Freq(rng As Range) As String

Dim cll As Range
Dim lngCount As Long
Dim lngMax As Long
Dim str As String

Set dic = CreateObject("scripting.dictionary")

On Error Resume Next

For Each cll In rng

If cll.Value <> "" Then
dic(cll.Value) = dic(cll.Value) + 1
lngCount = dic(cll.Value)
If lngCount > lngMax Then
lngMax = lngCount
str = cll.Value
End If
End If
Next

On Error GoTo 0

Freq = str

End Function

- - -

Does anyone know what needs to be changed to identify the 2nd, 3rd, 4th, 5th etc? Or a different way of doing it?

Thanks!
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Ok, how about
VBA Code:
Sub zoersdn()
   Dim Ary As Variant, Kth As Variant
   Dim r As Long, i As Long, NxtRw As Long
   Dim Dic As Object
  
   Set Dic = CreateObject("scripting.dictionary")
   Dic.CompareMode = 1
   Ary = Range("N11:X21").Value2
   NxtRw = UBound(Ary) + 4
  
   For r = 2 To UBound(Ary)
      For i = 1 To UBound(Ary, 2)
         If Ary(r, i) <> "" Then
            Dic(Ary(r, i)) = Dic(Ary(r, i)) + 1
         End If
      Next i
   Next r
   ReDim Ary(1 To Dic.Count, 1 To 2)
   Kth = Application.Large(Dic.Items, 10)
   If IsError(Kth) Then Kth = Application.min(Dic.Items)
   r = 0
  
   For i = 0 To Dic.Count - 1
      If Dic.Items()(i) >= Kth Then
         r = r + 1
         Ary(r, 1) = Dic.Keys()(i)
         Ary(r, 2) = Dic.Items()(i)
      End If
   Next i
   Range("N" & NxtRw).Resize(r, 2).Value = Ary
End Sub
 
Upvote 0
Do any of the names exist more than once in the N11:X21 range?
 
Upvote 0
In that case check the none of the names have leading/trailing spaces and that they are spelt exactly the same (case does not matter)
 
Upvote 0
They're all in exactly the same format (cases, spelling, space etc). Also, I only want to see the top 10.
 
Upvote 0
What happens if you have multiple people who are tied?
 
Upvote 0
They can still be included. The code you used before seemed to pull the correct data and was closer to what I'm looking for but the numbers were slightly off.
 
Upvote 0

Forum statistics

Threads
1,215,831
Messages
6,127,145
Members
449,363
Latest member
Yap999

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