ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   String Frequency (https://www.excelbanter.com/excel-programming/423308-string-frequency.html)

Brad

String Frequency
 
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?

OssieMac

String Frequency
 
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


--
Regards,

OssieMac


"Brad" wrote:

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?


Mike H

String Frequency
 
Perhaps like this

=INDEX(E1:E100,MODE(IF(E1:E100<"",MATCH(E1:E100,E 1: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

"OssieMac" wrote:

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


--
Regards,

OssieMac


"Brad" wrote:

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?



All times are GMT +1. The time now is 05:26 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com