Finding the most frequently used words/phrases

Chickon007

New Member
Joined
Feb 26, 2017
Messages
1
So I was given a database with a little over 600 entries and told to find the most common responses.


First I found the most common words using the method described in this thread:

https://www.mrexcel.com/forum/excel...rds-column-without-specifying-what-words.html

Basically, if you have text in a column like this:
Hi, I'm new here.
Hi, I've been here.
Hi, its Sunday.

<tbody>
</tbody>

It would return:
Hi - 3
here - 2
I'm - 1
I've - 1

<tbody>
</tbody>

etc. until it has gone through all of the words.

However, I would like it to look not only at individual words but phrases.

So instead of only looking at each word individually, it would also look at and measure the frequency of series of words.

Ex.

Hi
Hi I'm
Hi I'm new

I'm just wondering if there would be a way to do this by modifying the code in the linked spreadsheet (i'll copy and paste it below). I've done some minor stuff with coding macros in Excel and I can usually figure stuff out myself but I was hoping someone more experienced could provide some insight.

Option Explicit


Sub MakeWordList()
Dim InputSheet As Worksheet
Dim WordListSheet As Worksheet
Dim PuncChars As Variant, x As Variant
Dim i As Long, r As Long
Dim txt As String
Dim wordCnt As Long
Dim AllWords As Range
Dim PC As PivotCache
Dim PT As PivotTable

Application.ScreenUpdating = False
Set InputSheet = ActiveSheet
Set WordListSheet = Worksheets.Add(after:=Worksheets(Sheets.Count))
WordListSheet.Range("A1") = "All Words"
WordListSheet.Range("A1").Font.Bold = True
InputSheet.Activate
wordCnt = 2
PuncChars = Array(".", ",", ";", ":", "'", "!", "#", _
"$", "%", "&", "(", ")", " - ", "_", "--", "+", _
"=", "~", "/", "", "{", "}", "[", "]", """", "?", "*")
r = 1


' Loop until blank cell is encountered
Do While Cells(r, 1) <> ""
' covert to UPPERCASE
txt = UCase(Cells(r, 1))
' Remove punctuation
For i = 0 To UBound(PuncChars)
txt = Replace(txt, PuncChars(i), "")
Next i
' Remove excess spaces
txt = WorksheetFunction.Trim(txt)
' Extract the words
x = Split(txt)
For i = 0 To UBound(x)
WordListSheet.Cells(wordCnt, 1) = x(i)
wordCnt = wordCnt + 1
Next i
r = r + 1
Loop

' Create pivot table
WordListSheet.Activate
Set AllWords = Range("A1").CurrentRegion
Set PC = ActiveWorkbook.PivotCaches.Add _
(SourceType:=xlDatabase, _
SourceData:=AllWords)
Set PT = PC.CreatePivotTable _
(TableDestination:=Range("C1"), _
TableName:="PivotTable1")
With PT
.AddDataField .PivotFields("All Words")
.PivotFields("All Words").Orientation = xlRowField
End With
End Sub


Thanks for any advice!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
This macro will count the number of occurrences of whatever is entered into each cell in Column A.
Presently, it will not break down the phrases like you are seeking"

Hi
Hi I'm
Hi I'm new

But it will give you a count of the entire phrases in the column:

Hi, I'm new here..........1
Hi, I've been here........2
Hi, its Sunday.............2
Hi, I've been here.
Hi, its Sunday.

<tbody>
</tbody>

<tbody>
</tbody>


Code:
Option Explicit


Sub Special_Countif()
Dim i, LastRowA, LastRowB
LastRowA = Range("A" & Rows.Count).End(xlUp).Row
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Columns("B:C").ClearContents
    For i = 1 To LastRowA
        If Application.CountIf(Range("B:B"), Cells(i, "A")) = 0 Then
            Cells(i, "B").Offset(1, 0).Value = Cells(i, "A").Value
        End If
    Next
        Columns("B:B").Select
        Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
        LastRowB = Range("B" & Rows.Count).End(xlUp).Row
        For i = 2 To LastRowB
        Cells(i, "C").Value = Application.CountIf(Range("A:A"), Cells(i, "B"))
    Next i
    Range("B1").Value = "Entry"
    Range("C1").Value = "Occurrences"
    Range("B1:C1").HorizontalAlignment = xlCenter
    Range("B1").Select
    Columns("B:C").AutoFit
    Application.EnableEvents = True
End Sub


Paste the macro into a routine module, put a command button on Sheet 1. Enter terms and phrases in Column A.

Click button.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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