below is a pretty ugly and inefficient UDF
Function ModeWord(rngS As Range, Optional lngRank As Long = 1) As String
Dim oDic As Object, RegExp As Object, RegExpMatch As Object
Dim rngC As Range
Dim lngKey As Long, lngInstance As Long
Dim vTemp As Variant, vKeys As Variant, vKey As Variant
Dim strTemp As String
Set oDic = CreateObject("Scripting.Dictionary")
Set RegExp = CreateObject("vbscript.regexp")
With RegExp
.Global = True
.IgnoreCase = True
.Pattern = "\w+"
End With
For Each rngC In rngS.Cells
Set RegExpMatch = RegExp.Execute(Application.Trim(rngC.Value))
For lngInstance = 1 To RegExpMatch.Count Step 1
strTemp = LCase(RegExpMatch(lngInstance - 1))
With oDic
If Not .exists(strTemp) Then
.Add strTemp, 1 + 1 - (1 + .Count) / 10000
Else
.Item(strTemp) = .Item(strTemp) + 1
End If
End With
Next lngInstance
Next rngC
Set RegExpMatch = Nothing
With oDic
If lngRank <= .Count Then
ReDim vKeys(1 To .Count, 1 To 2)
For Each vKey In .Keys
lngKey = lngKey + 1
vKeys(lngKey, 1) = vKey
vKeys(lngKey, 2) = .Item(vKey)
Next vKey
vTemp = Application.Match(Application.Large(Application.Index(vKeys, 0, 2), lngRank), Application.Index(vKeys, 0, 2), 0)
ModeWord = vKeys(vTemp, 1) & " (" & Int(vKeys(vTemp, 2)) & ")"
End If
End With
Set oDic = Nothing
End Function
Using your example strings in A1:A3 then
A5:
=MODEWORD($A$1:$A$3,ROWS(A$5:A5))
copied down as far as desired
IMO you'd be better off using a Sub Routine with an Input dialog to specify no. items required.
Bookmarks