Help with Dictionary Class to arrange frequencies in the correct row.

MarsComet

New Member
Joined
Feb 14, 2014
Messages
26
Hello Guys,

I have macro that uses the Dictionary Class in VBA to generate a list of words in a specific sheet and its frecuency. I want to improve on this and update a separate workbook that stores those frequencies each time they are run. I know I can take adavantage that everything is already stored in the dictionary so that it can arrange in the workbook the new "frequency".

Example:

My macro generates a list like this on a new sheet when the code runs:

WordFrequency
apple20
banana15
orange10

<tbody>
</tbody>

I need that information also pasted in another workbook "WordFreq" in which I could log each time the macro was run.

So if in my workbook "WordFreq" I had the following:

WordNovember-01
apple50
orange25
peach5

<tbody>
</tbody>

I would like the macro to id if the word already exists and put that frequency in the correct row: if the word does not exists it should create it. so in the end it the result would be:

WordNovember-01November-14
apple5020
orange2510
peach50
banana015

<tbody>
</tbody>

That way I could pivot results and identify trends! :) My current code is as follows:

Code:
Sub WordFrequency()
    Dim Ws As Worksheet
    Dim wsa As Worksheet
    
  Dim X As Long, StopWords As Variant, Wrd As Variant, vNum As Variant, Txt As String
  StopWords = Array("a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also", "although", "always", "am", "among", "amongst", "amoungst", "amount", "an", "and", "another", "any", "anyhow", "anyone", "anything", "anyway", "anywhere", "are", "around", "as", "at", "back", "be", "became", "because", "become", "becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom", "but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven", "else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fifty", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", _
                    "four", "from", "front", "full", "further", "get", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own", "part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", _
                    "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves")
      Application.DisplayAlerts = False
    On Error Resume Next
        Sheets("Analysis").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
    Set Ws = ActiveSheet
    Sheets.Add.Name = "Analysis"
    Set wsa = ActiveSheet
    Ws.Select
  
  Dim Found As Range, clm As Long
Set Found = Rows(1).Find(what:="SHORT_DESCRIPTION", LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then
MsgBox "Short_Description not found."
Exit Sub
End If
clm = Found.Column
  
  Txt = Join(Application.Transpose(Range(Cells(2, clm), Cells(60000, clm))))
  Txt = " " & LCase(Txt & " " & Join(Application.Transpose(Range(Cells(60001, clm), Cells(120000, clm))))) & " "
  For X = 1 To Len(Txt)
    If Mid(Txt, X, 1) Like "[!A-Za-z0-9]" Then Mid(Txt, X) = " "
  Next
  For Each Wrd In StopWords
    Txt = Replace(Txt, " " & Wrd & " ", " ")
  Next
    For Each vNum In Array(64570081, 9841, 121, 13, 5, 3, 3, 2)
    Txt = Replace(Txt, Space(vNum), " ")
  Next
  Wrd = Split(Txt)
  With CreateObject("Scripting.Dictionary")
    For X = 0 To UBound(Wrd)
      If Len(Wrd(X)) > 2 Then .Item(Wrd(X)) = .Item(Wrd(X)) + 1
    Next
    On Error GoTo SomethingWentWrong
    Application.ScreenUpdating = False
    wsa.Cells(1, 1).Value = "Word"
    wsa.Cells(1, 2).Value = "Frequency"
    wsa.Range("A2:A" & .Count) = Application.Transpose(.Keys)
    wsa.Range("B2:B" & .Count) = Application.Transpose(.Items)
  End With
  wsa.Select
  Columns("A:B").Sort Columns("B"), xlDescending
  
 ActiveSheet.ListObjects.Add(xlSrcRange, Range("a1").CurrentRegion, , xlYes).Name = _
 "wrdf"


 ActiveSheet.ListObjects("wrdf").TableStyle = "TableStyleLight2"
 Columns("A:B").ColumnWidth = 15


SomethingWentWrong:
  Application.ScreenUpdating = True
  Beep
End Sub

Thanks in advance for the help!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I've managed to fin a Solution to this, however it takes 3xtimes longer for the code to run. Any help will be appreciated.

I managed to find the way to correctly state: "For each key in Dictionary, search in a field, if you find it, Paste the Item of that key in an Offset cell, if you dont find it, add it to the key list.

Code:
Sub WordFrequency()    Dim Ws As Worksheet
    Dim wsa As Worksheet
    
  Dim X As Long, StopWords As Variant, Wrd As Variant, vNum As Variant, Txt As String
  StopWords = Array("a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also", "although", "always", "am", "among", "amongst", "amoungst", "amount", "an", "and", "another", "any", "anyhow", "anyone", "anything", "anyway", "anywhere", "are", "around", "as", "at", "back", "be", "became", "because", "become", "becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom", "but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven", "else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fifty", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", _
                    "four", "from", "front", "full", "further", "get", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own", "part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", _
                    "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves")
      Application.DisplayAlerts = False
    On Error Resume Next
        Sheets("Analysis").Delete
    On Error GoTo 0
    Application.DisplayAlerts = True
    Set Ws = ActiveSheet
    Sheets.Add.Name = "Analysis"
    Set wsa = ActiveSheet
    Ws.Select
  
  Dim Found As Range, clm As Long
Set Found = Rows(1).Find(what:="SHORT_DESCRIPTION", LookIn:=xlValues, lookat:=xlWhole)
If Found Is Nothing Then
MsgBox "Short_Description not found."
Exit Sub
End If
clm = Found.Column
  
  Txt = Join(Application.Transpose(Range(Cells(2, clm), Cells(60000, clm))))
  Txt = " " & LCase(Txt & " " & Join(Application.Transpose(Range(Cells(60001, clm), Cells(120000, clm))))) & " "
  For X = 1 To Len(Txt)
    If Mid(Txt, X, 1) Like "[!A-Za-z0-9]" Then Mid(Txt, X) = " "
  Next
  For Each Wrd In StopWords
    Txt = Replace(Txt, " " & Wrd & " ", " ")
  Next
    For Each vNum In Array(64570081, 9841, 121, 13, 5, 3, 3, 2)
    Txt = Replace(Txt, Space(vNum), " ")
  Next
  Wrd = Split(Txt)
  Set dict = CreateObject("Scripting.Dictionary")
  With dict
    For X = 0 To UBound(Wrd)
      If Len(Wrd(X)) > 2 Then .Item(Wrd(X)) = .Item(Wrd(X)) + 1
    Next
    'On Error GoTo SomethingWentWrong
    Application.ScreenUpdating = False
    wsa.Cells(1, 1).Value = "Word"
    wsa.Cells(1, 2).Value = "Frequency"
    wsa.Range("A2:A" & .Count) = Application.Transpose(.Keys)
    wsa.Range("B2:B" & .Count) = Application.Transpose(.Items)
  End With
  wsa.Select
  Columns("A:B").Sort Columns("B"), xlDescending
  
 ActiveSheet.ListObjects.Add(xlSrcRange, Range("a1").CurrentRegion, , xlYes).Name = _
"wrdf"


ActiveSheet.ListObjects("wrdf").TableStyle = "TableStyleLight2"
Columns("A:B").ColumnWidth = 15
  
Dim repo As Workbook, tmpl As Workbook
Dim sh As Worksheet
    
    
    Set repo = ActiveWorkbook
    Set sh = ActiveSheet
    path = "C:\Users\" & Environ("Username") & "\Desktop\"


    Workbooks.Open Filename:=path & "Data Analysis\WordFrequency"
    Set WF = ActiveWorkbook
    
    WFCOL = Range("zz1").End(xlToLeft).Offset(0, 1).Column
    Range("zz1").End(xlToLeft).Offset(0, 1).Value = Date
[COLOR=#ff0000]    [/COLOR]
[COLOR=#ff0000]For Each Key In dict.Keys[/COLOR]
[COLOR=#ff0000]
[/COLOR]
[COLOR=#ff0000]Set KEYFIND = Range("A2:A" & Range("A1000000").End(xlUp).Row).Find(what:=Key, LookIn:=xlValues, lookat:=xlWhole)[/COLOR]
[COLOR=#ff0000]    If KEYFIND Is Nothing Then[/COLOR]
[COLOR=#ff0000]        Range("A1000000").End(xlUp).Offset(1, 0).Value = Key[/COLOR]
[COLOR=#ff0000]        Range("A1000000").End(xlUp).Offset(0, 1).Value = Item[/COLOR]
[COLOR=#ff0000]    Else[/COLOR]
[COLOR=#ff0000]        KEYFIND.Offset(0, WFCOL - 1) = dict(Key)[/COLOR]
[COLOR=#ff0000]    End If[/COLOR]
[COLOR=#ff0000]Next[/COLOR]
  
SomethingWentWrong:
  Application.ScreenUpdating = True
  Beep
End Sub
 
Upvote 0
There's a workbook at https://app.box.com/s/fkowtna5k76pp804tiqe that does this. For example, this is (part of) the histogram of the words on this page, excluding this post:

A​
B​
C​
1​
Unique
Output List
Total
2​
592​
1,844​
3​
#
Word
Freq
4​
1​
to
33​
5​
2​
the
29​
6​
3​
in
27​
7​
4​
as
24​
8​
5​
Txt
24​
9​
6​
a
20​
10​
7​
on
18​
11​
8​
Range
18​
12​
9​
for
17​
13​
10​
if
17​
14​
11​
Application
16​
15​
12​
with
16​
16​
13​
Wrd
16​
17​
14​
X
16​
18​
15​
is
15​
19​
16​
next
14​
20​
17​
this
14​
21​
18​
wsa
14​
22​
19​
end
13​
23​
20​
Cells
12​
24​
21​
clm
12​
25​
22​
found
12​
26​
23​
it
12​
27​
24​
reply
11​
28​
25​
Set
11​
29​
26​
code
10​
30​
27​
dictionary
10​
31​
28​
Dim
10​
32​
29​
each
10​
33​
30​
I
10​
34​
31​
that
10​
35​
32​
ActiveSheet
9​
36​
33​
then
9​
37​
34​
thread
9​
38​
35​
are
8​

(I just copied the page and pressed the Input button.)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,428
Members
448,896
Latest member
MadMarty

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