View Single Post
  #19   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Harlan Grove[_2_] Harlan Grove[_2_] is offline
external usenet poster
 
Posts: 1,231
Default how to count if cell "contains" a word

"Rick Rothstein \(MVP - VB\)" wrote...
I guess I am missing your point. . . .


Clearly.

. . . The way I read the initial post, the OP was interested in
finding the 4-character sequences without regard to the commas
(I assumed they were list delimiters), . . .


Yes, it's PRECISELY because they'd be delimiters that it's A BIG
MISTAKE to discard them.

. . . so it seemed to me that not factoring them into the search
was the thing to do. . . .


Duh. Delimiters are usually CRITICAL. They're what allow for
distinguishing abcf a separate token sought from abcFUBAR a token not
necessarily being sought.

Regardless, your formula

=SUM(COUNTIF(A1:A5,{"*abcd*abcf*","*abcf*abcd*"}))

1. does too much work - if order is unimportant, then it's sufficient
to search for abcd and abcf, either as delimited tokens or simple
substrings, it's unnecessary to search for one then the other and the
other then the first;
2. is a bug in waiting - try your formula on the singe cell

0 ,abcx,abcd,abcf,abcd,xyz

In a single cell, should this be counted as 1 or 2?

Compare this to the results of my formula,

=COUNT(FIND(",abcd,",SUBSTITUTE(","&A1:A5&","," ",""))
+FIND(",abcf,",SUBSTITUTE(","&A1:A5&","," ","")))

If you don't like it as an array formula, make it

=SUMPRODUCT(--ISNUMBER(
FIND(",abcd,",SUBSTITUTE(","&A1:A5&","," ",""))
+FIND(",abcf,",SUBSTITUTE(","&A1:A5&","," ",""))
))