by Allen Wyatt
(last updated October 22, 2020)
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.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
One of the things you can do with macros is to work with disk files. As you do so, you may have a need to create a new ...Discover More
Want to know when a workbook was last modified? Want to put that date within the header of your worksheet? Here's how to ...Discover More
Does your macro need to allow the user to specify a particular file name that should be used by the macro? Here's a quick ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.