Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 846
Default 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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default 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?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default 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?

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting the frequency of a text string Michael Styles Excel Discussion (Misc queries) 2 May 19th 09 07:08 PM
Frequency of a word string in an excel database Danny_McCaslin Excel Discussion (Misc queries) 2 December 13th 06 06:11 PM
Importing Long String - String Manipulation (INVRPT) (EDI EANCOM 96a) Brian Excel Programming 3 February 9th 06 03:38 PM
histograms - frequency and relative frequency? confusedstudent Excel Discussion (Misc queries) 2 February 8th 06 08:20 AM
frequency of characters within a string Nick Excel Programming 4 July 24th 03 07:52 PM


All times are GMT +1. The time now is 06:41 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"