Generating a Keyword Occurrence List

by Allen Wyatt
(last updated December 3, 2016)

3

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, and 2016.

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

Deleting Every X Rows

Grab some info from a source other than Excel, and you may find the need to delete a certain pattern of rows from a ...

Discover More

Inserting a Paragraph from within a Macro

Macros are often used to process documents, resulting in changes of one manner or another. If you need your macro to add ...

Discover More

Extracting E-mail Addresses from Hyperlinks

If you have a list of hyperlinked e-mail addresses in a worksheet, you may want to extract the addresses from those ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Sheets for Months

One common type of workbook used in offices is one that contains a single worksheet for each month of the year. If you need ...

Discover More

Putting an X in a Clicked Cell

Need to click on a cell and have it replaced with an "X"? Macros make it easy to do, as illustrated in this tip.

Discover More

Relative VBA Selections

Need to select a cell using a macro? Need that selection to be relative to the cell you currently have selected? Here's the ...

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}] 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 four less than 9?

2016-12-04 07:59:49

Michael (Micky) Avidan

It is possible that I did not fully understand the question (presented 8 days a go to Allen's E-Mail subscribers - but the following picture was my solution (without VBA).
Did I wrongly interpreted the task ?
(see Figure 1 below)
--------------------------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2017)
ISRAEL

Figure 1. 


2016-12-03 14:09:08

Clive Darling

Guys, I really loved this macro. I've been compiling a history of an amateur theatre and have been sourcing cast lists for approx 280 plays back to 1936. I had entered the cast list for any one play in a single cell separated by commas. I amended your macro to change the "space" to a "comma" and voila! I have a list of each actor and the number of plays he/she performed in over the course of the 80 years!
Great macro for me anyway!!


2016-12-03 10:39:52

Simon Hemmings

Hi there
This is a brilliant tip, one thing I would ask is, is there a way that you could get this macro to work between two specific dates?

Any help appreciated

Thank you
Simon


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.