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:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Word[/TD]
[TD]Frequency[/TD]
[/TR]
[TR]
[TD]apple[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]banana[/TD]
[TD]15[/TD]
[/TR]
[TR]
[TD]orange[/TD]
[TD]10[/TD]
[/TR]
</tbody>[/TABLE]

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:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Word[/TD]
[TD]November-01[/TD]
[/TR]
[TR]
[TD]apple[/TD]
[TD]50[/TD]
[/TR]
[TR]
[TD]orange[/TD]
[TD]25[/TD]
[/TR]
[TR]
[TD]peach[/TD]
[TD]5[/TD]
[/TR]
</tbody>[/TABLE]

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:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Word[/TD]
[TD]November-01[/TD]
[TD]November-14[/TD]
[/TR]
[TR]
[TD]apple[/TD]
[TD]50[/TD]
[TD]20[/TD]
[/TR]
[TR]
[TD]orange[/TD]
[TD]25[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]peach[/TD]
[TD]5[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]banana[/TD]
[TD]0[/TD]
[TD]15[/TD]
[/TR]
</tbody>[/TABLE]

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

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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:

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


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

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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