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
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.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Do you want a way to reverse names within a cell, making them "last, first" instead of "first last?" Here's a handy macro ...
Discover MoreWhen creating macros, you'll often have a need to select different cells in the worksheet. Here's how to select the first ...
Discover MoreWhen you enter information into a workbook, Excel automatically recalculates every worksheet in every open workbook on ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments