Counting occurance of numbers in a cell

LeeT

New Member
Joined
Feb 17, 2010
Messages
10
Hi, I have an array formula that counts occurances of a certain number in a cell:

=SUMPRODUCT(LEN(","&Sheet1!$G8&",")-LEN(SUBSTITUTE(","&Sheet1!$G8&",",",1,",",,")))

The above looks in G8 and counts how many '1's occur. (if i'm looking for a double digit number i just divide by 2).

Problem is it works fine when numbers are entered like:1,4,1,5,7,10,etc. But if I put in:1,1,3,5,3,7, it only counts the number 1 once - I think its because the 1's are next to each other, because otherwise it works.

Anyone any idea how to make this work - if the same numbers are next to each other??
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
try this:
Code:
=(LEN(","&SUBSTITUTE(Sheet1!G8,",",",,")&",")-LEN(SUBSTITUTE(","&SUBSTITUTE(Sheet1!G8,",",",,")&",",",1,",",,")))
 
Upvote 0
I use this UDF for the same purpose:

Code:
Public Function CountNums(rngMyRange As Range, lngVar As Long) As Long

Dim myArr
Dim lngItem As Long
Dim lngCount As Long

myArr = Application.Transpose(Split(rngMyRange, ","))

For lngItem = 1 To UBound(myArr, 1)

    If myArr(lngItem, 1) = lngVar Then CountNums = CountNums + 1

Next lngItem

End Function

Dom
 
Upvote 0

Forum statistics

Threads
1,224,509
Messages
6,179,192
Members
452,893
Latest member
denay

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