Wednesday, July 17, 2024

# Counting words/elements correctly in Excel

In some of my previous posts, you’ve already seen my Python examples on how to count words accurately in a document or in blocks of text (search for: Wordcloud). It’s also possible to count the words in Excel, but we have some gotchas there to be aware of. In this blog, I demonstrate some of those and how to properly count words in different scenarios.

Let’s say you have a block of text that has bullets such as below.

In Excel, we can get the number of words in text by taking the entire length of the text (assume it’s in a cell), find the length of texts containing only spaces, then deduct that from the total length, and finally add 1. e.g. `=LEN(A16)-LEN(SUBSTITUTE(A16," ",""))+1`

When we apply this to the above content, we get 96 (content is in cell A16 in this example). If we manually count it, or verify it in Microsoft Word, we see the count is actually 84! Obviously the bullet items are causing issues here. Let’s take the following example…

Using the formula above, we get 90, whereas it’s actually 84. The tickmarks are unicode symbols and should not be counted as normative words. So, how do we get around this? Is there a way for Excel to report the correct number of count? Yes, there is. The trick is to apply TRIM which will remove trailing and leading spaces and some non-ANSI characters. So, if we apply this formula: `=LEN(TRIM(A20))-LEN(SUBSTITUTE(A20," ","")) +1` we see the numbers to be exactly 84, which is what Word is reporting as well.

So far so good. What if we had a series of elements (words or values) in a cell that are separated by some delimiting character (comma, space, semicolon, whatever)? Can we get the number of elements from that cell in Excel? (If you search for my posts on “tkinter” you will find some of earlier posts where I save a bunch of coordinates and hex color codes in a file, delimited by a character, and then I read the file, parse it, and draw them on screen…using Python). Yes, in Excel, we can also get the count of those elements and even parse them and put individual element in its own cell (this is done by Text to Columns feature in Excel). However, to get the count automatically, we need to again resort to some functions.

The idea is the same as above for counting words, except this time we need to split them using a different delimiter. So if the delimiting character is specified in cell B3, we can apply this formula to get the exact count of elements in the long text in cell A1 for example: `=LEN(A1)-LEN(SUBSTITUTE(A1,\$B\$3,""))+1`

Where A1 content may look like this:

So, essentially, we counted the number of colors specified in the long string in A1 cell. For the curious readers, below is the code for Python where I enter a file name (with path if it’s not in a current directory, or even on the network), specify any delimiter (default: space) and it’ll spit out the total number of words in that text file.

and a sample session and output from that code:

Happy counting!

``````
▛Interested in creating programmable, cool electronic gadgets? Give my newest book on Arduino a try: Hello Arduino!
▟``````