Excel Count Functions

G

Gasbag

I am trying to count the number of cells in a row that contain one or more of
three letters?
 
G

Gary Mc

I'm sure there are other and probably better solutions but this will work
=SUM(COUNTIF(B2:E2,"*a*"),COUNTIF(B2:E2,"*x*"))
Hope this is helpful
GMc
 
R

Rick Rothstein

Give this array-entered** formula a try...

=SUM(IF(ISNUMBER(SEARCH("a",A1:A100)),1,IF(ISNUMBER(SEARCH("b",A1:A100)),1,IF(ISNUMBER(SEARCH("c",A1:A100)),1,0))))

**Commit this formula using Ctrl+Shift+Enter, not just Enter by itself.

Note: Change the "a", "b", and "c" to the letters you want to find (keep the
letters in quotes when you do).
 
M

Mike H

Someone must be able to do better than this

call with

=CountChar(B2:E2,"a","b","c")

Function CountChar(rng As Range, ch1 As String, ch2 As String, ch3 As
String) As Long
For Each c In rng
If InStr(UCase(c.Value), UCase(ch1)) Then CountChar = CountChar + 1
If InStr(UCase(c.Value), UCase(ch2)) Then CountChar = CountChar + 1
If InStr(UCase(c.Value), UCase(ch3)) Then CountChar = CountChar + 1
Next
End Function

Mike
 
R

Rick Rothstein

Of course, you said "in a row". Try this array-entered formula instead...

=SUM(IF(ISNUMBER(SEARCH("a",1:1)),1,IF(ISNUMBER(SEARCH("b",1:1)),1,IF(ISNUMBER(SEARCH("c"1:1)),1,0))))

**Commit this formula using Ctrl+Shift+Enter, not just Enter by itself.

The 1:1 in each part of the formula means Row 1... change them all (there
are 3 of them) to the row you are interested in (for example 4:4 for Row 4).
And, of course, still change the individual "a", "b", and "c" letters to the
letters you want to look for.
 
R

Rick Rothstein

You can simplify your function like this...

Function CountChr(Rng As Range, C1 As String, _
C2 As String, C3 As String) As Long
Dim C As Range
For Each C In Rng
If C.Value Like "*[" & C1 & C2 & C3 & "]*" Then CountChr = CountChr + 1
Next
End Function

Note I shortened the name of your function and some of your argument names
in order to prevent newsreaders from word-wrapping the long If-Then
statement.
 
M

Mike H

Thanks rick,

I continually miss the option of putting values in an array and using Like.

Mike

Rick Rothstein said:
You can simplify your function like this...

Function CountChr(Rng As Range, C1 As String, _
C2 As String, C3 As String) As Long
Dim C As Range
For Each C In Rng
If C.Value Like "*[" & C1 & C2 & C3 & "]*" Then CountChr = CountChr + 1
Next
End Function

Note I shortened the name of your function and some of your argument names
in order to prevent newsreaders from word-wrapping the long If-Then
statement.

--
Rick (MVP - Excel)


Mike H said:
Someone must be able to do better than this

call with

=CountChar(B2:E2,"a","b","c")

Function CountChar(rng As Range, ch1 As String, ch2 As String, ch3 As
String) As Long
For Each c In rng
If InStr(UCase(c.Value), UCase(ch1)) Then CountChar = CountChar + 1
If InStr(UCase(c.Value), UCase(ch2)) Then CountChar = CountChar + 1
If InStr(UCase(c.Value), UCase(ch3)) Then CountChar = CountChar + 1
Next
End Function

Mike
 
R

Rick Rothstein

I forgot the upper/lower case stuff. Here is a more general approach that
will allow the user to look for more (or less) than three characters and
which takes care of the letter casing problem as well...

Function CountChr(Rng As Range, ParamArray Char()) As Long
Dim X As Long
Dim C As Range
Dim Chars As String
For X = LBound(Char) To UBound(Char)
Chars = Chars & UCase(Char(X))
Next
For Each C In Rng
If UCase(C.Value) Like "*[" & Chars & "]*" Then CountChar = CountChar +
1
Next
End Function

Now the user can do this...

=CountChr(A1:Z1,"a","b")

or this...

=CountChr(A1:Z1,"a","b","c","d","e")

etc.

--
Rick (MVP - Excel)


Rick Rothstein said:
You can simplify your function like this...

Function CountChr(Rng As Range, C1 As String, _
C2 As String, C3 As String) As Long
Dim C As Range
For Each C In Rng
If C.Value Like "*[" & C1 & C2 & C3 & "]*" Then CountChr = CountChr + 1
Next
End Function

Note I shortened the name of your function and some of your argument names
in order to prevent newsreaders from word-wrapping the long If-Then
statement.

--
Rick (MVP - Excel)


Mike H said:
Someone must be able to do better than this

call with

=CountChar(B2:E2,"a","b","c")

Function CountChar(rng As Range, ch1 As String, ch2 As String, ch3 As
String) As Long
For Each c In rng
If InStr(UCase(c.Value), UCase(ch1)) Then CountChar = CountChar + 1
If InStr(UCase(c.Value), UCase(ch2)) Then CountChar = CountChar + 1
If InStr(UCase(c.Value), UCase(ch3)) Then CountChar = CountChar + 1
Next
End Function

Mike
 
L

Lars-Åke Aspelin

I am trying to count the number of cells in a row that contain one or more of
three letters?

Here is an alternative to already presented solution.
This one is not an array formula:

=SUMPRODUCT(1-ISERROR(FIND("a",A1:J1))*ISERROR(FIND("b",A1:J1))*ISERROR(FIND("c",A1:J1)))

a,b,c to be replaced with your specific letters.
A1:J1 to be adjusted to cover you width of your data row.
Note that FIND is case sensitive so if you want to distinguish letter
"a" from letter "A" in your data FIND it is a better choice than
SEARCH which is not case sensitive.

Hope this helps / Lars-Åke
 
H

Harlan Grove

Gasbag said:
I am trying to count the number of cells in a row that contain one or more of
three letters?

Not particularly general, but the following may be the shortest
formula that would do this.

=COUNT(1/(MMULT({1,1,1},--(SUBSTITUTE(A1:p1,{"a";"g";"m"},"")=A1:p1))
<3))

This doesn't have to be entered as an array formula. You could make it
more general. Define a name (I'll use X) referring to either a
constant array of the letters sought or to a 1-column by multiple row
range and use an array formula like

=COUNT(1/(MMULT(TRANSPOSE(CODE(X)^0),--(SUBSTITUTE(A1:p1,X,"")=A1:p1))
<COUNTA(X)))

for a more general approach.
 
H

Harlan Grove

Rick Rothstein said:
You can simplify your function like this...

Function CountChr(Rng As Range, C1 As String, _
                   C2 As String, C3 As String) As Long
  Dim C As Range
  For Each C In Rng
    If C.Value Like "*[" & C1 & C2 & C3 & "]*" Then CountChr = CountChr + 1
  Next
End Function
....

Or make it general. Like

Function foo(a As Variant, p As String) As Double
Dim x As Variant
If Not TypeOf a Is Range And Not IsArray(a) Then a = Array(a)
For Each x In a
If x Like p Then foo = foo + 1
Next x
End Function

which could be used in formulas like

=foo(A1:p1,"*[agm]*")

This would allow counting any valid LIKE pattern.
 
R

Rick Rothstein

As written, your formula is case-sensitive. If the OP requires a
case-insensitive solution, perhaps this modification will do...

=COUNT(1/(MMULT({1,1,1},--(SUBSTITUTE(LOWER(A1:p1),{"a";"g";"m"},"")=A1:p1))<3))
 
R

Rick Rothstein

I like this idea better than my paramarray suggestion; however, I would
change your function slightly so the user would not have to know the syntax
of the Like operator...

Function Foo(A As Variant, P As String) As Double
Dim X As Variant
If Not TypeOf A Is Range And Not IsArray(A) Then A = Array(A)
For Each X In A
If X Like "*[" & P & "]*" Then Foo = Foo + 1
Next X
End Function

With this variation, all the user has to do is call the function like
this...

=Foo(A1:p1,"agm")

--
Rick (MVP - Excel)


Rick Rothstein said:
You can simplify your function like this...

Function CountChr(Rng As Range, C1 As String, _
C2 As String, C3 As String) As Long
Dim C As Range
For Each C In Rng
If C.Value Like "*[" & C1 & C2 & C3 & "]*" Then CountChr = CountChr + 1
Next
End Function
....

Or make it general. Like

Function foo(a As Variant, p As String) As Double
Dim x As Variant
If Not TypeOf a Is Range And Not IsArray(a) Then a = Array(a)
For Each x In a
If x Like p Then foo = foo + 1
Next x
End Function

which could be used in formulas like

=foo(A1:p1,"*[agm]*")

This would allow counting any valid LIKE pattern.
 
R

Rick Rothstein

I just noticed that I forgot to provide for case insensitivity. How about we
make that optional (and change the function and arguments to something more
meaningful)...

Function Foo(A As Variant, ByVal P As String, _
Optional C As Boolean) As Double
Dim X As Variant
If Not TypeOf A Is Range And Not IsArray(A) Then A = Array(A)
If C Then P = LCase(P) & UCase(P)
For Each X In A
If X Like "*[" & P & "]*" Then Foo = Foo + 1
Next X
End Function

As set up, the function is case sensitive; if the user wants the count to be
case insensitive, they just need to include True for the 3rd argument. For
example...

=Foo(A1:p1,"agm",TRUE)

--
Rick (MVP - Excel)


Rick Rothstein said:
I like this idea better than my paramarray suggestion; however, I would
change your function slightly so the user would not have to know the syntax
of the Like operator...

Function Foo(A As Variant, P As String) As Double
Dim X As Variant
If Not TypeOf A Is Range And Not IsArray(A) Then A = Array(A)
For Each X In A
If X Like "*[" & P & "]*" Then Foo = Foo + 1
Next X
End Function

With this variation, all the user has to do is call the function like
this...

=Foo(A1:p1,"agm")

--
Rick (MVP - Excel)


Rick Rothstein said:
You can simplify your function like this...

Function CountChr(Rng As Range, C1 As String, _
C2 As String, C3 As String) As Long
Dim C As Range
For Each C In Rng
If C.Value Like "*[" & C1 & C2 & C3 & "]*" Then CountChr = CountChr + 1
Next
End Function
...

Or make it general. Like

Function foo(a As Variant, p As String) As Double
Dim x As Variant
If Not TypeOf a Is Range And Not IsArray(a) Then a = Array(a)
For Each x In a
If x Like p Then foo = foo + 1
Next x
End Function

which could be used in formulas like

=foo(A1:p1,"*[agm]*")

This would allow counting any valid LIKE pattern.
 
A

Ashish Mathur

Hi,

You can also try something like this. In a separate range , day D10:D12,
type the 3 letters. Now suppose your entries are in A1:A50. in cell B1,
type this array formula (Ctrl+Shift+Enter) and copy down

=1*OR(ISNUMBER(SEARCH($D$10:$D$12,A1,1)))

Now simply sum up column B.

--
Regards,

Ashish Mathur
Microsoft Excel MVP
www.ashishmathur.com
 
T

T. Valko

It also counts cells that are numeric:

gmail...10...AAA...<empty>...3M

If there might be both text and numbers then you need to add a test for
text:

=SUMPRODUCT(--(MMULT({1,1,1},--(SUBSTITUTE(LOWER(A1:p1),{"a";"g";"m"},"")=A1:p1))<3),--(ISTEXT(A1:p1)))

If there are errors in the range that'll return the error and the COUNT
version(s) will return 0.

gmail...10...AAA...#N/A...3M
 
H

Harlan Grove

Rick Rothstein said:
I like this idea better than my paramarray suggestion; however, I would
change your function slightly so the user would not have to know the syntax
of the Like operator...
....

IMO, better for users to know the syntax for LIKE operator patterns
since that would allow significantly greater flexibility and
functionality than simply searching for alternative single characters.
 
H

Harlan Grove

T. Valko said:
It also counts cells that are numeric:

gmail...10...AAA...<empty>...3M

If there might be both text and numbers then you need to add a test for
text:

=SUMPRODUCT(--(MMULT({1,1,1},--(SUBSTITUTE(LOWER(A1:p1),
{"a";"g";"m"},"")=A1:p1))<3),--(ISTEXT(A1:p1)))
....

No, better to use ISNUMBER(SEARCH(...)).

=COUNT(1/MMULT({1,1,1},--ISNUMBER(SEARCH({"a";"g";"m"},A1:p1))))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top