

To prove this, we would get the following counts if we used our current solution on the problematic text string. ", our formula would return an improper count and as such, it is necessary for us to remove any leading or trailing whitespace. What I am referring to is the case where there might be extra spaces included at the end of our text string.įor example, if I had the text string, "Sally Jumps. With our formula complete, some of you may have realized there is still one thing which could go wrong with our calculations and something we forgot to consider. The formula looks like the following:ĬountOfLengthWithoutSpaces, LEN(SUBSTITUTE(textString, " ", "")),ĬountOfLength - countOfLengthWithoutSpaces + 1

In this case, our LAMBDA will only take one input, the text string, and will need to define two names within the LET that map to our intermediate calculations. Moving along, all that is left to do, is encode this formula as a LAMBDA so we can use it later without having to think through this problem again! Putting together the intermediate calculations we created, (and assuming our text-string lives in A1) gives us the following solution: With both problems completed, we can now begin to construct the final version of our formula. If we take that formula and replace it with the "SallyJumps." portion of CountOfLengthWithoutSpaces we can calculate this without modifying the original string!

The formula would look like the following. To remove whitespaces from our text string, we can use the SUBSTITUTE function to search for any spaces: " " and replace them with nothing: "". The next thing to consider is how we can calculate a version of our text with no whitespaces and thankfully there is a formula for that! In summary, the formula we are going to build roughly looks like this: This is a simple thing to fix and important to remember when authoring our final formula. You would be correct and with this calculation, we will always be off by 1. “OK, we get 1, but that is wrong… There are clearly 2 words in that sentence!”. With those two values calculated, we can simply subtract the LengthWithoutSpaces from the CountofLength and that will give us the value 1. In this text string the counts are as follows and generally speaking the LEN function is good for this: To prove this, lets take a simple example of “Sally Jumps.”. This should give us a valid count of how many “words” are in our text string.
#Word counter excel series
Below you can see a series of text strings which I have picked for this example.īreaking the problem down, what we are looking to do is find individual words in our sentence, but, what makes something a word? Well, naively speaking, we know that there is a word because there is a space before and/or after a series of characters.īecause of this, we can count the length of our string with and without spaces and then subtract the two. Hopefully, if you’re like me and find yourself making use of this formula from time to time, it gives you a more repeatable way to call it without having to write all the logic from scratch.įor this example, lets start with the problem we have at hand and would like to abstract as a custom function. I find myself making use of this formula all the time when I need to do text scrubbing or fact checking I have edited my data correctly. In today’s example we will be creating a lambda which allows you to count how many words are in a text string. If you didn’t catch the announcement, be sure to check out the blog post highlighting the release of this new function: LAMBDA Additionally, you have the opportunity to engage with us on the lambdas you have built yourself and of course let us know how our own formulas could be improved. This is intended to highlight lambdas we have cooked up that show the power of this new super-charged function. This post is the second of a series where we will be sharing out examples of lambdas.
