Counting words (text strings) in a range

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,532
Office Version
  1. 365
Platform
  1. Windows
I know CountIf can count the number of occurrences of a word in a range if each call in that range contains only a single word. Is there a formula that can count the number of occurrences of a word in a range where each cell may contains multiple words?

In this table, I would like the "house" count to be "3".
R/CCDE
4house1D4: =COUNTIF($C$4:$C$7,"house")
5dog1D5: =COUNTIF($C$4:$C$7,"dog")
6house dog0D6: =COUNTIF($C$4:$C$7,"cat")
7cat house

<tbody>
</tbody>

Thanks
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You can use a wildcard character or sumproduct with search/find

I don't understand the sumproduct solution, but the wildcard solution does just what I wanted.
R/CCDEF
4househouse2E4: =COUNTIF($C$4:$C$7,"*" & $D4 & "*")
5dogdog3E5: =COUNTIF($C$4:$C$7,"*" & $D5 & "*")
6house dogcat1E6: =COUNTIF($C$4:$C$7,"*" & $D6 & "*")
7dog cat

<tbody>
</tbody>

Thank you

-j
 
Upvote 0
So long as you are happy to have, for example, "cat" counted in a cell that contains "Tom was scathing"

Sigh. There is always a catch. :(

For my immediate needs, it's fine. I am making a large family photo collage. For each photo that I add to the collage, I add the list of names of people in the photo, so I keep it more or less "even". I don't want to have Aunt Edna in 25 photos and Aunt Irma in only 5. All of the names are unique and we don't have any nested names (Bill & Billy).

But suppose I wanted to only count complete words (so not count "Bill" in "Billy"). How would you do that?
 
Upvote 0
Try:

=SUMPRODUCT(ISNUMBER(SEARCH(" "&D4&" "," "&$C$4:$C$7&" "))+0)
The above formula only works if each word (name) being looked for is surrounded by spaces... it will not pick up words (names) adjacent to non-letters such as in text like these...

Bob, Bill, Joe and Sally

A beautiful view (Bill and Joe on the rock, Bob leaning on the tree)

I have not tested this fully, but I believe the following formula will work no matter what non-alpha character surrounds the word in cell D4.

=SUMPRODUCT((NOT(ISNUMBER(FIND(UPPER(MID("="&$C$4:$C$7&"=",SEARCH(D4,$C$4:$C$7),1)),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")))*NOT(ISNUMBER(FIND(UPPER(MID("="&$C$4:$C$7&"=",SEARCH(D4,$C$4:$C$7)+LEN(D4)+1,1)),"ABCDEFGHIJKLMNOPQRSTUVWXYZ")))))
 
Last edited:
Upvote 0
But suppose I wanted to only count complete words (so not count "Bill" in "Billy"). How would you do that?
If there is no punctuation, then Aladin has provided a solution.

If there is punctuation & you want the solution to be robust, then with great difficult if not impossibility. Excel is much more suited to dealing with numbers than words. Rick's suggestion seems to have something drastically wrong. :eek:

Excel Workbook
CDE
4MaryJim4
5Bob, Bill, Joe and SallyBill3
6Sam BillyMary4
7Ken
Sheet1
 
Last edited:
Upvote 0
If there is punctuation & you want the solution to be robust, then with great difficult if not impossibility. Excel is much more suited to dealing with numbers than words. Rick's suggestion seems to have something drastically wrong. :eek:
Wow, I really screwed that up, didn't I? I am not entirely sure what happened as the formula I developed (which I am guessing was not the one I ended up posting) seemed to work in my tests. Unfortunately, the worksheet it was on is long gone, so I cannot check if I copied it correctly or not. Anyway, let me stick with code (which I am far more comfortable with than formulas) and post this UDF which I believe will return the correct count of cells where adjacent non-alpha characters do not affect the count. The UDF takes two mandatory arguments... the first is the text to search for and the second is the range to search for it in... and one optional argument... which allows the search to be case sensitive or not (default is for an insensitive search).
Code:
[table="width: 500"]
[tr]
	[td]Function CountWholeWords(FindMe As String, RangeToSearch As Range, _
                         Optional CaseSensitive As Boolean) As Variant
  Dim X As Long, CV As String, FM As String, Pattern As String, Cell As Range
  If Len(FindMe) = 0 Then
    CountWholeWords = ""
    Exit Function
  End If
  For Each Cell In Intersect(RangeToSearch, RangeToSearch.Parent.UsedRange)
    If Len(Cell.Value) Then
      If CaseSensitive Then
        CV = Cell.Value
        FM = FindMe
        Pattern = "[!0-9A-Za-z]"
      Else
        CV = UCase(Cell.Value)
        FM = UCase(FindMe)
        Pattern = "[!0-9A-Z]"
      End If
      For X = 1 To Len(CV) - Len(FM) + 1
        If Mid(" " & CV & " ", X, Len(FM) + 2) Like Pattern & FM & Pattern Then
          CountWholeWords = CountWholeWords + 1
        End If
      Next
    End If
  Next
End Function[/td]
[/tr]
[/table]


HOW TO INSTALL UDFs
------------------------------------
If the OP (or readers of this thread) are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use CountWholeWords just like it was a built-in Excel function. For example,

=CountWholeWords(D4,$C$4:$C$7)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Last edited:
Upvote 0
.. this UDF which I believe will return the correct count of cells...
I'm still not so sure - ref count for Mary below with your function in column E.

I have suggested another UDF, used in column F, though I wouldn't be surprised if examples can be found where it fails too.
My function assumes a single column range to be searched as per the OP's original sample.

Code:
Function CountWord(rng As Range, wrd As String) As Long
  With CreateObject("VBScript.RegExp")
    .Global = True
    .IgnoreCase = True
    .Pattern = "@.*?\b" & Replace(Replace(wrd, "-", "#"), "'", "%") & "(?=[ ,\.\?\!:;@]|$)"
    CountWord = .Execute("@" & Replace(Replace(Join(Application.Transpose(rng), "@"), "-", "#"), "'", "%")).Count
  End With
End Function

Excel Workbook
CDEF
4Mary Sue Bill.Mary-Lou11
5Bob, Bill, Joe, Billy and SallyMary32
6Mary-Lou, Ken, MaryBill22
7Sam00
Sheet2
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,984
Members
449,058
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top