String Frequency

B

Brad

I'm trying to write a program that will take a column of strings and return
the string that occurs most in the list. Any suggestions?
 
O

OssieMac

Hi Brad,

It won't surprise me if someone comes up with a method of doing this with
formulas directly on the worksheet. However, as you asked for code, here is
an example.

Sub FrequencyOfStr()

Dim rngColumn As Range
Dim strSave As String
Dim lngCount As Long
Dim lngSave As Long
Dim c As Range

'Edit "Sheet1" in following line to your sheet
With Sheets("Sheet1")

'Edit "E" in thefollowing code to your column Id
Set rngColumn = Range(.Cells(1, "E"), _
.Cells(.Rows.Count, "E").End(xlUp))
End With

For Each c In rngColumn
lngCount = WorksheetFunction. _
CountIf(rngColumn, c.Value)

If lngCount > lngSave Then
lngSave = lngCount
strSave = c.Value
End If
Next c

MsgBox "Most frequent string = " & strSave & Chr(13) & _
"Number of occurrences = " & lngSave

End Sub
 
M

Mike H

Perhaps like this

=INDEX(E1:E100,MODE(IF(E1:E100<>"",MATCH(E1:E100,E1:E100,0))))

This is an array formula which must be entered by pressing CTRL+Shift+Enter
'and not just Enter. If you do it correctly then Excel will put curly brackets
'around the formula {}. You can't type these yourself. If you edit the formula
'you must enter it again with CTRL+Shift+Enter.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top