How do you put =LEFT() inside =COUNTIF() in Excel?

Steve_Brady
Steve_Brady used Ask the Experts™
on
Hello,

How do you correctly put a =LEFT() function inside of a =COUNTIF() function?

For example, here's a formula I just tried (which obviously does not work):

=COUNTIF(LEFT(L4:L33963,2),L2)

My objective is to determine how many of the entries in the range begin with the two characters:  "##" (i.e. not variables but two number signs) and have the result display in L3.  Therefore, I entered "##" in L2 and the above formula in L3.  

In other words, I don't want to count the cells whose complete entries are the same as L3 but only those whose first two characters are the same.  To do that, it seems like there should be an equal sign in there somewhere saying, "if the first two characters in a given row equal the value in L2 then count that row" but I can't determine where it would go.

Sometimes these formulas just fall right into place and other times it seems as though there is a 12 inch block wall (full of grout) standing in the way!  :p

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010
Commented:
Try:

=COUNTIF(L4:L33963,L2&"*")
Most Valuable Expert 2012
Top Expert 2012
Commented:
Good one.

Here's another:

=SUMPRODUCT(--(LEFT(L4:L33963,2)=L2))
Most Valuable Expert 2013
Commented:
Note that the reason thiis formula is invalid

=COUNTIF(LEFT(L4:L33963,2),L2)

.....is that the first argument of COUNTOF must be a range reference (not an array). Once you use a function, like LEFT, on a range it becomes an array.

Note that Patrick's version uses a "wildcard" and wildcards don't work with numeric values, e.g. if L2 is a 12 and L4 is a numeric (not text-formatted) 123 then the COUNTIF formula won't count it......

regards barry
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

We were taught to split complex problems in excel into smaller parts... I've never felt like I would run out of space in a spreadsheet. Try putting the left function in another column and then referencing THAT column with your countif function. If it annoys you, you can always hide the column so you don't have to look at it. Then it will work, and it will not have the weakness of the wildcard method as explained above.

Thanks to barryhoudini for some great information. I never knew why certain things didn't work, I always just found work arounds. Very cool.
Rob HensonFinance Analyst

Commented:
You could also convert it to an array formula by confirming entry with Shift + Ctrl + Enter.

There are numerous trains of thought about array formulas, probably worth avoiding where there is another option.

Thanks
Rob H

Author

Commented:
Multiple helpful responses!  Thanks

>>Thanks to barryhoudini for some great information....Very cool.

Not meant to embarrass him but FYI, Barry's answers are always accurate, always straightforward, always easy to follow and understand, and always include "great information."  He is a good one to follow around!

Afterthought:  perhaps "always" is a tad bit too strong -- I know Barry would say so -- so perhaps we should change them to "almost always" but if we do, note that the "tad" is a very small one indeed!  :)

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial