Generating a Keyword Occurrence List

Written by Allen Wyatt (last updated November 5, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


5

Martyn has a worksheet that includes, in column B, quite a few keywords. Any individual cell could have any number of unique keywords, each separated by a space. He needs a way to generate a list of unique keywords along with how many rows contain each keyword.

There is no simple way to generate the keyword list using formulas. You can, however, make quick work of such a list using a macro. The following is an example that will do the job very quickly.

Sub KeywordList()
    Dim dTally As Dictionary
    Dim rSource As Range
    Dim c As Range
    Dim d As Variant
    Dim aKeys() As String
    Dim J As Integer
    Dim sTemp As String

    Set dTally = New Dictionary
    Set rSource = Selection
    For Each c In rSource
        ' Put all keywords in an array
        aKeys = Split(c, " ")
        For J = LBound(aKeys) To UBound(aKeys)
            sTemp = LCase(Trim(aKeys(J)))
            If Len(sTemp) > 0 Then
                If dTally.Exists(sTemp) Then
                    ' Increment existing keyword
                    dTally(sTemp) = dTally(sTemp) + 1
                Else
                    ' Add new keyword and count
                    dTally.Add sTemp, 1
                End If
            End If
        Next J
        Erase aKeys
    Next c

    ' Now have all the keywords in a dictionary object
    ' Add a new worksheet to store the list
    Worksheets.Add
    Cells(1, 1) = "Keyword"
    Cells(1, 2) = "Count"

    J = 1
    For Each d In dTally.Keys
        J = J + 1
        Cells(J, 1) = d
        Cells(J, 2) = dTally(d)
    Next d
End Sub

If the macro doesn't work on your system, it could be because Excel doesn't recognize the Dictionary object. If you suspect this is the case, make sure you enable the Microsoft Scripting Runtime library within VBA. (You do this in the Visual Basic Editor by clicking Tools | References, locating the library, clicking the check box next to it, and finally clicking OK.)

In order to use the macro, simply select the cells that contain the keywords you want to tally (in Martyn's case, that would be the cells in column B) and then run the macro. It creates a new worksheet that lists the keywords in column A and, in column B, how many times each keyword occurs.

As written, the macro pays no attention to case of the keywords; this means, for instance, that "Orange" is the same as "orange." If you want the keyword list to be case sensitive, then you simply need to remove the LCase statement; it is used only once in the macro.

You should also be aware that the keywords are listed in the new worksheet in the order in which they occur in the selection you make before running the macro. Once in the worksheet, you can easily sort the keywords, if desired.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13499) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Finding Out the Folder for an Open Document

If you work with a lot of documents at the same time, it can be difficult to remember the folder in which any given ...

Discover More

Excluding Some Data from a Chart

Excel is a whiz at creating charts from your worksheet data. When the program tries to determine what should be included ...

Discover More

Jumping to the End of the Document in Error

If you inadvertently move to the end of the document, you might be wondering how to get your insertion point back to ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Saving Information in a Text File

The VBA programming language provided with Excel allows you to create and modify text files quite easily. Here's how to ...

Discover More

Macro Fails after Filter

When developing a macro that others may use, you might want to test it out to make sure it works properly if a filter is ...

Discover More

Deleting Worksheet Code in a Macro

When creating an application in VBA for others to use, you might want a way for your VBA code to modify or delete other ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 + 7?

2022-11-11 17:03:43

J. Woolley

Assuming the text with keywords is in cells B2:B100, here's another way using Excel 365 formulas. (Earlier versions of Excel might not include all of these functions.)
Put this formula in cell C2:
=SORT(TEXTSPLIT(TEXTJOIN(" ",TRUE,B2:B100),," ",TRUE))
Put this formula in cell D2:
=UNIQUE($C$2#)
Notice $C$2# represents the spilled range (array) starting at cell $C$2.
Put this formula in cell E2:
=COUNTIF($C$2#,"="&D2)
Notice $C$2 is absolute and D2 is relative.
Click the bottom-right corner of cell E2 and drag down to the last non-blank row of column D to duplicate the formula in cell E2, so each Keyword in column D has a Count in column E.
(see Figure 1 below)

Figure 1. 


2022-11-10 11:12:11

J. Woolley

It is easy to convert the KeywordList macro described in my previous comment into a ListKeywords dynamic array function for use with Excel 365 or 2021+.

Function ListKeywords(Target As Range)
Dim Tally As Collection, T As Variant, C As Range
Dim sKeys() As String, sTemp As String, n As Integer
Dim bExists As Boolean
Dim A() As Variant
Set Tally = New Collection
For Each C In Target
...
Next C
ReDim A(1 To Tally.Count, 1 To 2)
n = 0
For Each T In Tally
n = n + 1
A(n, 1) = T(0)
A(n, 2) = T(1)
Next T
ListKeywords = A
End Function

Notes:
1. Code represented by the ellipsis (...) is unchanged from the KeywordList macro.
2. The function's result does not include a header and is not sorted.
3. Here are some example cell formulas:
=ListKeywords(B2:B100)
=ListKeywords(Sheet2!B2:B100)
=ListKeywords((Sheet2!B2:B100,Sheet2!D2:D100))
=SORT(ListKeywords((Sheet2!B2:B100,Sheet2!D2:D100)))


2022-11-09 05:43:30

Sandeep

excellent uses of dictionary and collection.


2022-11-08 12:46:41

J. Woolley

This version of the Tip's macro uses Collection instead of Dictionary, therefore, it does not require a special library and should work on a Mac.

Sub KeywordList()
Dim Tally As Collection, T As Variant, c As Range
Dim sKeys() As String, sTemp As String, n As Integer
Dim bExists As Boolean
Set Tally = New Collection
For Each c In Selection
    sTemp = LCase(WorksheetFunction.Trim(c))
    sKeys = Split(sTemp, " ")
    For n = LBound(sKeys) To UBound(sKeys)
        sTemp = sKeys(n)
        On Error Resume Next
            T = Tally(sTemp)
            bExists = (Err = 0)
        On Error GoTo 0
        If bExists Then
            T(1) = T(1) + 1
            Tally.Remove sTemp
            Tally.Add T, sTemp
        Else
            Tally.Add VBA.Array(sTemp, 1), sTemp
        End If
    Next n
Next c
Sheets.Add After:=ActiveSheet
Cells(1, 1) = "Keyword"
Cells(1, 2) = "Count"
n = 1
For Each T In Tally
    n = n + 1
    Cells(n, 1) = T(0)
    Cells(n, 2) = T(1)
Next T
[A:B].Sort [A1], Header:=xlYes
End Sub

Notes:
1. WorksheetFunction.Trim(Text) and VBA.Trim(Text) produce different results when Text has multiple spaces between words.
2. A Collection can have unique text Keys like a Dictionary, but it does not have a method to determine if a Key exists in the Collection.
3. Dictionary items can be manipulated like array elements, but Collection items cannot.
4. VBA.Array is always base 0 (unaffected by Option Base).
5. The macro's result is sorted alphabetically by keyword.


2022-11-07 02:45:27

Philip

For Mac users, by definition the "Dictionary" concept is NOT recognised in VBA. There are some work-arounds for this available online using Class definitions which mimic the behaviour of a Dictionary, but they are not for the faint-hearted.

Check out https://www.ozgrid.com/forum/index.php?thread/102697-office-2011-for-mac-dictionary-replacement/&postID=1226536#post1226536


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.